Esempio n. 1
0
        public static NSAttributedString ToAttributedString(this FormattedText ft)
        {
            NSMutableAttributedString ns = new NSMutableAttributedString(ft.Text);

            ns.BeginEditing();
            foreach (var att in ft.Attributes)
            {
                var r = new NSRange(att.StartIndex, att.Count);
                if (att is BackgroundTextAttribute)
                {
                    var xa = (BackgroundTextAttribute)att;
                    ns.AddAttribute(NSAttributedString.BackgroundColorAttributeName, xa.Color.ToNSColor(), r);
                }
                else if (att is ColorTextAttribute)
                {
                    var xa = (ColorTextAttribute)att;
                    ns.AddAttribute(NSAttributedString.ForegroundColorAttributeName, xa.Color.ToNSColor(), r);
                }
                else if (att is UnderlineTextAttribute)
                {
                    var xa    = (UnderlineTextAttribute)att;
                    int style = xa.Underline ? 0x01 /*NSUnderlineStyleSingle*/ : 0;
                    ns.AddAttribute(NSAttributedString.UnderlineStyleAttributeName, (NSNumber)style, r);
                }
                else if (att is FontStyleTextAttribute)
                {
                    var xa = (FontStyleTextAttribute)att;
                    if (xa.Style == FontStyle.Italic)
                    {
                        Messaging.void_objc_msgSend_int_NSRange(ns.Handle, applyFontTraits.Handle, (IntPtr)(long)NSFontTraitMask.Italic, r);
                    }
                    else if (xa.Style == FontStyle.Oblique)
                    {
                        ns.AddAttribute(NSAttributedString.ObliquenessAttributeName, (NSNumber)0.2f, r);
                    }
                    else
                    {
                        ns.AddAttribute(NSAttributedString.ObliquenessAttributeName, (NSNumber)0.0f, r);
                        Messaging.void_objc_msgSend_int_NSRange(ns.Handle, applyFontTraits.Handle, (IntPtr)(long)NSFontTraitMask.Unitalic, r);
                    }
                }
                else if (att is FontWeightTextAttribute)
                {
                    var xa    = (FontWeightTextAttribute)att;
                    var trait = xa.Weight >= FontWeight.Bold ? NSFontTraitMask.Bold : NSFontTraitMask.Unbold;
                    Messaging.void_objc_msgSend_int_NSRange(ns.Handle, applyFontTraits.Handle, (IntPtr)(long)trait, r);
                }
                else if (att is LinkTextAttribute)
                {
                    var xa = (LinkTextAttribute)att;
                    ns.AddAttribute(NSAttributedString.LinkAttributeName, new NSUrl(xa.Target.ToString()), r);
                    ns.AddAttribute(NSAttributedString.ForegroundColorAttributeName, NSColor.Blue, r);
                    ns.AddAttribute(NSAttributedString.UnderlineStyleAttributeName, NSNumber.FromInt32((int)NSUnderlineStyle.Single), r);
                }
                else if (att is StrikethroughTextAttribute)
                {
                    var xa    = (StrikethroughTextAttribute)att;
                    int style = xa.Strikethrough ? 0x01 /*NSUnderlineStyleSingle*/ : 0;
                    ns.AddAttribute(NSAttributedString.StrikethroughStyleAttributeName, (NSNumber)style, r);
                }
                else if (att is FontTextAttribute)
                {
                    var xa = (FontTextAttribute)att;
                    var nf = ((FontData)Toolkit.GetBackend(xa.Font)).Font;
                    ns.AddAttribute(NSAttributedString.FontAttributeName, nf, r);
                }
            }
            ns.EndEditing();
            return(ns);
        }
Esempio n. 2
0
        public static NSMutableAttributedString ToAttributedString(this FormattedText ft)
        {
            NSMutableAttributedString ns = new NSMutableAttributedString(ft.Text);

            ns.BeginEditing();
            foreach (var att in ft.Attributes)
            {
                var r = new NSRange(att.StartIndex, att.Count);
                if (att is BackgroundTextAttribute)
                {
                    var xa = (BackgroundTextAttribute)att;
                    ns.AddAttribute(NSStringAttributeKey.BackgroundColor, xa.Color.ToNSColor(), r);
                }
                else if (att is ColorTextAttribute)
                {
                    var xa = (ColorTextAttribute)att;
                    ns.AddAttribute(NSStringAttributeKey.ForegroundColor, xa.Color.ToNSColor(), r);
                }
                else if (att is UnderlineTextAttribute)
                {
                    var xa    = (UnderlineTextAttribute)att;
                    int style = xa.Underline ? 0x01 /*NSUnderlineStyleSingle*/ : 0;
                    ns.AddAttribute(NSStringAttributeKey.UnderlineStyle, (NSNumber)style, r);
                }
                else if (att is FontStyleTextAttribute)
                {
                    var xa = (FontStyleTextAttribute)att;
                    if (xa.Style == FontStyle.Italic)
                    {
                        Messaging.void_objc_msgSend_int_NSRange(ns.Handle, applyFontTraits.Handle, (IntPtr)(long)NSFontTraitMask.Italic, r);
                    }
                    else if (xa.Style == FontStyle.Oblique)
                    {
                        ns.AddAttribute(NSStringAttributeKey.Obliqueness, (NSNumber)0.2f, r);
                    }
                    else
                    {
                        ns.AddAttribute(NSStringAttributeKey.Obliqueness, (NSNumber)0.0f, r);
                        Messaging.void_objc_msgSend_int_NSRange(ns.Handle, applyFontTraits.Handle, (IntPtr)(long)NSFontTraitMask.Unitalic, r);
                    }
                }
                else if (att is FontWeightTextAttribute)
                {
                    var xa    = (FontWeightTextAttribute)att;
                    var trait = xa.Weight >= FontWeight.Bold ? NSFontTraitMask.Bold : NSFontTraitMask.Unbold;
                    Messaging.void_objc_msgSend_int_NSRange(ns.Handle, applyFontTraits.Handle, (IntPtr)(long)trait, r);
                }
                else if (att is FontSizeTextAttribute)
                {
                    var xa = (FontSizeTextAttribute)att;
                    ns.EnumerateAttribute(NSStringAttributeKey.Font, r, NSAttributedStringEnumeration.None, (NSObject value, NSRange range, ref bool stop) => {
                        var font = value as NSFont;
                        if (font == null)
                        {
                            font = NSFont.SystemFontOfSize(xa.Size);
                        }
                        else
                        {
                            font = font.WithSize(xa.Size);
                        }
                        ns.RemoveAttribute(NSStringAttributeKey.Font, r);
                        ns.AddAttribute(NSStringAttributeKey.Font, font, r);
                    });
                }
                else if (att is LinkTextAttribute)
                {
                    var xa = (LinkTextAttribute)att;
                    if (xa.Target != null)
                    {
                        ns.AddAttribute(NSStringAttributeKey.Link, new NSUrl(xa.Target.ToString()), r);
                    }
                    ns.AddAttribute(NSStringAttributeKey.ForegroundColor, Toolkit.CurrentEngine.Defaults.FallbackLinkColor.ToNSColor(), r);
                    ns.AddAttribute(NSStringAttributeKey.UnderlineStyle, NSNumber.FromInt32((int)NSUnderlineStyle.Single), r);
                }
                else if (att is StrikethroughTextAttribute)
                {
                    var xa    = (StrikethroughTextAttribute)att;
                    int style = xa.Strikethrough ? 0x01 /*NSUnderlineStyleSingle*/ : 0;
                    ns.AddAttribute(NSStringAttributeKey.StrikethroughStyle, (NSNumber)style, r);
                }
                else if (att is FontTextAttribute)
                {
                    var xa = (FontTextAttribute)att;
                    var nf = ((FontData)Toolkit.GetBackend(xa.Font)).Font;

                    ns.EnumerateAttribute(NSStringAttributeKey.Font, r, NSAttributedStringEnumeration.None, (NSObject value, NSRange range, ref bool stop) => {
                        var font = value as NSFont;
                        if (font == null)
                        {
                            font = nf;
                        }
                        else
                        {
                            var w      = NSFontManager.SharedFontManager.WeightOfFont(font);
                            var traits = NSFontManager.SharedFontManager.TraitsOfFont(font);
                            font       = NSFontManager.SharedFontManager.FontWithFamily(nf.FamilyName, traits, w, font.PointSize);
                        }
                        ns.RemoveAttribute(NSStringAttributeKey.Font, r);
                        ns.AddAttribute(NSStringAttributeKey.Font, font, r);
                    });
                }
            }
            ns.EndEditing();
            return(ns);
        }