Esempio n. 1
0
            void AddAttributeInternal(TextAttribute attribute)
            {
                var r = new NSRange(attribute.StartIndex, attribute.Count);

                if (attribute is BackgroundTextAttribute)
                {
                    var xa = (BackgroundTextAttribute)attribute;
                    TextStorage.AddAttribute(NSStringAttributeKey.BackgroundColor, xa.Color.ToNSColor(), r);
                }
                else if (attribute is ColorTextAttribute)
                {
                    var xa = (ColorTextAttribute)attribute;
                    TextStorage.AddAttribute(NSStringAttributeKey.ForegroundColor, xa.Color.ToNSColor(), r);
                }
                else if (attribute is UnderlineTextAttribute)
                {
                    var xa    = (UnderlineTextAttribute)attribute;
                    var style = xa.Underline ? NSUnderlineStyle.Single : NSUnderlineStyle.None;
                    TextStorage.AddAttribute(NSStringAttributeKey.UnderlineStyle, NSNumber.FromInt32((int)style), r);
                }
                else if (attribute is FontStyleTextAttribute)
                {
                    var xa = (FontStyleTextAttribute)attribute;
                    if (xa.Style == FontStyle.Italic)
                    {
                        TextStorage.ApplyFontTraits(NSFontTraitMask.Italic, r);
                    }
                    else if (xa.Style == FontStyle.Oblique)
                    {
                        // copy Pango.Style.Oblique behaviour (25% skew)
                        TextStorage.AddAttribute(NSStringAttributeKey.Obliqueness, NSNumber.FromFloat((float)0.25), r);
                    }
                    else
                    {
                        TextStorage.RemoveAttribute(NSStringAttributeKey.Obliqueness, r);
                        TextStorage.ApplyFontTraits(NSFontTraitMask.Unitalic, r);
                    }
                }
                else if (attribute is FontWeightTextAttribute)
                {
                    var     xa = (FontWeightTextAttribute)attribute;
                    NSRange er;
                    // get the effective font to modify for the given range
                    var ft = TextStorage.GetAttribute(NSStringAttributeKey.Font, attribute.StartIndex, out er, r) as NSFont;
                    ft = ft.WithWeight(xa.Weight);
                    TextStorage.AddAttribute(NSStringAttributeKey.Font, ft, r);
                }
                else if (attribute is LinkTextAttribute)
                {
                    TextStorage.AddAttribute(NSStringAttributeKey.ForegroundColor, Toolkit.CurrentEngine.Defaults.FallbackLinkColor.ToNSColor(), r);
                    TextStorage.AddAttribute(NSStringAttributeKey.UnderlineStyle, NSNumber.FromInt32((int)NSUnderlineStyle.Single), r);
                }
                else if (attribute is StrikethroughTextAttribute)
                {
                    var xa    = (StrikethroughTextAttribute)attribute;
                    var style = xa.Strikethrough ? NSUnderlineStyle.Single : NSUnderlineStyle.None;
                    TextStorage.AddAttribute(NSStringAttributeKey.StrikethroughStyle, NSNumber.FromInt32((int)style), r);
                }
                else if (attribute is FontTextAttribute)
                {
                    var xa = (FontTextAttribute)attribute;
                    var nf = ((FontData)ApplicationContext.Toolkit.GetSafeBackend(xa.Font)).Font;
                    TextStorage.AddAttribute(NSStringAttributeKey.Font, nf, r);
                }
            }