in FigmaSharp/FigmaSharp.Cocoa/Extensions/ViewConfigureExtensions.cs [134:193]
public static void Configure(this NSTextField label, FigmaText text, bool configureColor = true)
{
label.Alignment = text.style.textAlignHorizontal == "CENTER" ? NSTextAlignment.Center : text.style.textAlignHorizontal == "LEFT" ? NSTextAlignment.Left : NSTextAlignment.Right;
label.AlphaValue = text.opacity;
//label.LineBreakMode = NSLineBreakMode.ByWordWrapping;
//label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal);
if (label.Cell is VerticalAlignmentTextCell cell)
{
cell.VerticalAligment = text.style.textAlignVertical == "CENTER" ? VerticalTextAlignment.Center : text.style.textAlignVertical == "TOP" ? VerticalTextAlignment.Top : VerticalTextAlignment.Bottom;
}
if (!configureColor) {
return;
}
var fills = text.fills.FirstOrDefault();
if (fills != null && fills.visible) {
label.TextColor = fills.color.ToNSColor();
}
if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0)
{
var attributedText = new NSMutableAttributedString(label.AttributedStringValue);
for (int i = 0; i < text.characterStyleOverrides.Length; i++)
{
var range = new NSRange(i, 1);
var key = text.characterStyleOverrides[i].ToString();
if (!text.styleOverrideTable.ContainsKey(key) )
{
//we want the default values
attributedText.AddAttribute(NSStringAttributeKey.ForegroundColor, label.TextColor, range);
attributedText.AddAttribute(NSStringAttributeKey.Font, label.Font, range);
continue;
}
//if there is a style to override
var styleOverrided = text.styleOverrideTable[key];
//set the color
NSColor fontColorOverrided = label.TextColor;
var fillOverrided = styleOverrided.fills?.FirstOrDefault();
if (fillOverrided != null && fillOverrided.visible)
fontColorOverrided = fillOverrided.color.ToNSColor();
attributedText.AddAttribute(NSStringAttributeKey.ForegroundColor, fontColorOverrided, range);
//TODO: we can improve this
//set the font for this character
NSFont fontOverrided = label.Font;
if (styleOverrided.fontFamily != null)
{
fontOverrided = FigmaExtensions.ToNSFont(styleOverrided);
}
attributedText.AddAttribute(NSStringAttributeKey.Font, fontOverrided, range);
}
label.AttributedStringValue = attributedText;
}
}