private NSMutableAttributedString buildAttributedString(string text, Font font, StringFormat format = null, Color? fontColor=null) { // Create a new attributed string definition var ctAttributes = new CTStringAttributes (); // Font attribute ctAttributes.Font = font.nativeFont; // -- end font if (format != null && (format.FormatFlags & StringFormatFlags.DirectionVertical) == StringFormatFlags.DirectionVertical) { //ctAttributes.VerticalForms = true; } if (fontColor.HasValue) { // Font color var fc = fontColor.Value; var cgColor = new CGColor(fc.R / 255f, fc.G / 255f, fc.B / 255f, fc.A / 255f); ctAttributes.ForegroundColor = cgColor; ctAttributes.ForegroundColorFromContext = false; // -- end font Color } if (font.Underline) { // Underline #if MONOMAC int single = (int)MonoMac.AppKit.NSUnderlineStyle.Single; int solid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid; var attss = single | solid; ctAttributes.UnderlineStyleValue = attss; #else ctAttributes.UnderlineStyleValue = 1; #endif // --- end underline } if (font.Strikeout) { // StrikeThrough #if MONOMAC int single = (int)MonoMac.AppKit.NSUnderlineStyle.Single; int solid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid; var attss = single | solid; ctAttributes.UnderlineStyleValue = attss; #else ctAttributes.UnderlineStyleValue = 1; #endif // --- end StrikeThrough } var alignment = CTTextAlignment.Left; var alignmentSettings = new CTParagraphStyleSettings(); alignmentSettings.Alignment = alignment; var paragraphStyle = new CTParagraphStyle(alignmentSettings); ctAttributes.ParagraphStyle = paragraphStyle; // end text alignment NSMutableAttributedString atts = new NSMutableAttributedString(text,ctAttributes.Dictionary); return atts; }
private static NSMutableAttributedString buildAttributedString(string text, Font font, Color? fontColor=null) { // Create a new attributed string definition var ctAttributes = new CTStringAttributes (); // Font attribute ctAttributes.Font = font.nativeFont; // -- end font if (fontColor.HasValue) { // Font color var fc = fontColor.Value; var cgColor = new CGColor(fc.R / 255f, fc.G / 255f, fc.B / 255f, fc.A / 255f); ctAttributes.ForegroundColor = cgColor; ctAttributes.ForegroundColorFromContext = false; // -- end font Color } if (font.Underline) { // Underline #if MONOMAC int single = (int)MonoMac.AppKit.NSUnderlineStyle.Single; int solid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid; var attss = single | solid; ctAttributes.UnderlineStyleValue = attss; #else ctAttributes.UnderlineStyleValue = 1; #endif // --- end underline } if (font.Strikeout) { // StrikeThrough // NSColor bcolor = NSColor.Blue; // NSObject bcolorObject = new NSObject(bcolor.Handle); // attsDic.Add(NSAttributedString.StrikethroughColorAttributeName, bcolorObject); // #if MACOS // int stsingle = (int)MonoMac.AppKit.NSUnderlineStyle.Single; // int stsolid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid; // var stattss = stsingle | stsolid; // var stunderlineObject = NSNumber.FromInt32(stattss); // #else // var stunderlineObject = NSNumber.FromInt32 (1); // #endif // // attsDic.Add(StrikethroughStyleAttributeName, stunderlineObject); // --- end underline } // Text alignment var alignment = CTTextAlignment.Left; var alignmentSettings = new CTParagraphStyleSettings(); alignmentSettings.Alignment = alignment; var paragraphStyle = new CTParagraphStyle(alignmentSettings); ctAttributes.ParagraphStyle = paragraphStyle; // end text alignment NSMutableAttributedString atts = new NSMutableAttributedString(text,ctAttributes.Dictionary); return atts; }
private static NSMutableAttributedString buildAttributedString(string text, CTFont font, Color? fontColor=null) { // Create a new attributed string from text NSMutableAttributedString atts = new NSMutableAttributedString(text); var attRange = new NSRange(0, atts.Length); var attsDic = new NSMutableDictionary(); // Font attribute NSObject fontObject = new NSObject(font.Handle); attsDic.Add(FontAttributedName, fontObject); // -- end font if (fontColor.HasValue) { // Font color var fc = fontColor.Value; NSColor color = NSColor.FromDeviceRgba(fc.R / 255f, fc.G / 255f, fc.B / 255f, fc.A / 255f); NSObject colorObject = new NSObject(color.Handle); attsDic.Add(ForegroundColorAttributedName, colorObject); // -- end font Color } if (underLine) { // Underline int single = (int)MonoMac.AppKit.NSUnderlineStyle.Single; int solid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid; var attss = single | solid; var underlineObject = NSNumber.FromInt32(attss); //var under = NSAttributedString.UnderlineStyleAttributeName.ToString(); attsDic.Add(UnderlineStyleAttributeName, underlineObject); // --- end underline } if (strikeThrough) { // StrikeThrough // NSColor bcolor = NSColor.Blue; // NSObject bcolorObject = new NSObject(bcolor.Handle); // attsDic.Add(NSAttributedString.StrikethroughColorAttributeName, bcolorObject); int stsingle = (int)MonoMac.AppKit.NSUnderlineStyle.Single; int stsolid = (int)MonoMac.AppKit.NSUnderlinePattern.Solid; var stattss = stsingle | stsolid; var stunderlineObject = NSNumber.FromInt32(stattss); attsDic.Add(StrikethroughStyleAttributeName, stunderlineObject); // --- end underline } // Text alignment var alignment = CTTextAlignment.Left; var alignmentSettings = new CTParagraphStyleSettings(); alignmentSettings.Alignment = alignment; var paragraphStyle = new CTParagraphStyle(alignmentSettings); NSObject psObject = new NSObject(paragraphStyle.Handle); // end text alignment attsDic.Add(ParagraphStyleAttributeName, psObject); atts.SetAttributes(attsDic, attRange); return atts; }