示例#1
0
        public static string GetNSFontString(NativeControlVariant controlVariant, FigmaText text, bool withWeight = true)
        {
            var fontWeight = CocoaHelpers.GetNSFontWeight(text);

            if (controlVariant == NativeControlVariant.Regular)
            {
                // The system default Medium is slightly different, so let Cocoa handle that
                if (fontWeight == NSFontWeight.Medium || !withWeight)
                {
                    return($"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ GetNSFontSizeString(controlVariant) })");
                }
            }

            if (controlVariant == NativeControlVariant.Small)
            {
                // The system default Medium is slightly different, so let Cocoa handle that
                if (fontWeight == NSFontWeight.Medium || !withWeight)
                {
                    return($"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ GetNSFontSizeString(controlVariant) })");
                }
                else
                {
                    return($"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ GetNSFontSizeString(controlVariant) }, { GetNSFontWeightString(text) })");
                }
            }

            if (withWeight)
            {
                return($"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ GetNSFontSizeString(controlVariant) }, { GetNSFontWeightString(text) })");
            }
            else
            {
                return($"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ GetNSFontSizeString(controlVariant) })");
            }
        }
示例#2
0
        protected override IView OnConvertToView(FigmaNode currentNode, ProcessedNode parentNode, FigmaRendererService rendererService)
        {
            var label = new NSTextField();

            label.Editable                = false;
            label.DrawsBackground         = false;
            label.Bordered                = false;
            label.PreferredMaxLayoutWidth = 1;

            var frame = (FigmaFrame)currentNode;

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            currentNode.TryGetNativeControlType(out NativeControlType controlType);
            currentNode.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (text != null)
            {
                label.StringValue = text.characters;
                label.Alignment   = CocoaHelpers.GetNSTextAlignment(text);
                label.Font        = CocoaHelpers.GetNSFont(controlVariant, text);
            }

            if (controlType == NativeControlType.LabelHeader)
            {
                label.Font = NSFont.SystemFontOfSize(headerFontSize, CocoaHelpers.GetNSFontWeight(text));
            }

            foreach (var styleMap in text?.styles)
            {
                if (rendererService.FileProvider.TryGetStyle(styleMap.Value, out FigmaStyle style))
                {
                    if (styleMap.Key == "fill")
                    {
                        label.TextColor = CocoaHelpers.GetNSColor(style.name);
                    }
                }
            }

            return(new View(label));
        }