public override void Draw(CoreGraphics.CGRect rect) { base.Draw(rect); if (_path == null) { return; } using (CGContext g = UIGraphics.GetCurrentContext()) { //set up drawing attributes g.SetLineWidth(1); UIColor.Clear.SetFill(); UIColor.Clear.SetStroke(); var attrString = new NSMutableAttributedString(" "); if (_text != null) { attrString = new NSMutableAttributedString(_text); } // We'll Set the justification to center so it'll // print the number in the center of the erect CTParagraphStyle alignStyle = new CTParagraphStyle(new CTParagraphStyleSettings { Alignment = _textAligment //CTTextAlignment.Left }); // Calculate the range of the attributed string NSRange stringRange = new NSRange(0, attrString.Length); var font = new CTFont(_fontName, _fontSize); // Add style attributes to the attributed string attrString.AddAttributes(new CTStringAttributes { Font = font, ParagraphStyle = alignStyle, ForegroundColor = Foreground.CGColor }, stringRange); // Create a container for the attributed string using (var framesetter = new CTFramesetter(attrString)) { using (var frame = framesetter.GetFrame(stringRange, _path, null)) { g.SaveState(); g.TranslateCTM(0, TextHeight); g.ScaleCTM(1, -1); //g.RotateCTM (0.2f ); frame.Draw(g); g.RestoreState(); } } } }
private void DrawTemperatureNumber(double degree, double increase) { using (var font = new CTFont("Verdana", 10)) using (var context = UIGraphics.GetCurrentContext()) { UIColor.Clear.SetFill(); CGRect erect = new CGRect(increase - 15, (.7 * sViewHeight) + 20, 30, 20); // Fill and stroke the the circle first so the // number will be drawn on top of the circle // using (var path = new CGPath()) { // path.AddEllipseInRect (erect); // context.AddPath (path); // context.DrawPath (CGPathDrawingMode.FillStroke); // } // Then draw the number 'on top of' the circle using (var path = new CGPath()) using (var attrString = new NSMutableAttributedString(degree.ToString())) { // We'll Set the justification to center so it'll // print the number in the center of the erect CTParagraphStyle alignStyle = new CTParagraphStyle(new CTParagraphStyleSettings { Alignment = CTTextAlignment.Center }); // Calculate the range of the attributed string NSRange stringRange = new NSRange(0, attrString.Length); // Add style attributes to the attributed string attrString.AddAttributes(new CTStringAttributes { Font = font, ParagraphStyle = alignStyle, ForegroundColor = UIColor.Red.CGColor }, stringRange); // Create a container for the attributed string using (var framesetter = new CTFramesetter(attrString)) { erect.Y = -erect.Y; path.AddRect(erect); using (var frame = framesetter.GetFrame(stringRange, path, null)) { context.SaveState(); context.TranslateCTM((nfloat)0, erect.Height); context.ScaleCTM(1, -1); frame.Draw(context); context.RestoreState(); } } } } }
public void StylePropertiesTest() { var settings = new CTParagraphStyleSettings() { TailIndent = 5, ParagraphSpacingBefore = 5, ParagraphSpacing = 5, LineSpacing = 5, MinimumLineHeight = 5, MaximumLineHeight = 5, LineHeightMultiple = 5, DefaultTabInterval = 5, HeadIndent = 5, FirstLineHeadIndent = 5, LineBreakMode = CTLineBreakMode.TruncatingHead, BaseWritingDirection = CTWritingDirection.Natural, Alignment = CTTextAlignment.Justified, TabStops = new [] { new CTTextTab(CTTextAlignment.Justified, 2), new CTTextTab(CTTextAlignment.Natural, 1) } }; var style = new CTParagraphStyle(settings); Assert.DoesNotThrow(() => { Assert.AreEqual(settings.TailIndent, (nfloat)style.TailIndent, "TailIndent"); Assert.AreEqual(settings.ParagraphSpacingBefore, (nfloat)style.ParagraphSpacingBefore, "ParagraphSpacingBefore"); Assert.AreEqual(settings.ParagraphSpacing, (nfloat)style.ParagraphSpacing, "ParagraphSpacing"); Assert.AreEqual(settings.LineSpacing, (nfloat)style.LineSpacing, "LineSpacing"); Assert.AreEqual(settings.MinimumLineHeight, (nfloat)style.MinimumLineHeight, "MinimumLineHeight"); Assert.AreEqual(settings.MaximumLineHeight, (nfloat)style.MaximumLineHeight, "MaximumLineHeight"); Assert.AreEqual(settings.LineHeightMultiple, (nfloat)style.LineHeightMultiple, "LineHeightMultiple"); Assert.AreEqual(settings.DefaultTabInterval, (nfloat)style.DefaultTabInterval, "DefaultTabInterval"); Assert.AreEqual(settings.HeadIndent, (nfloat)style.HeadIndent, "HeadIndent"); Assert.AreEqual(settings.FirstLineHeadIndent, (nfloat)style.FirstLineHeadIndent, "FirstLineHeadIndent"); Assert.AreEqual(settings.LineBreakMode, style.LineBreakMode, "LineBreakMode"); Assert.AreEqual(settings.BaseWritingDirection, style.BaseWritingDirection, "LineBreakMode"); Assert.AreEqual(settings.Alignment, style.Alignment, "Alignment"); var styleTabStops = style.GetTabStops(); Assert.AreEqual(settings.TabStops.Count(), styleTabStops.Length, "TabStops"); Assert.True(styleTabStops.Any(t => t.Location == 2 && t.TextAlignment == CTTextAlignment.Justified)); Assert.True(styleTabStops.Any(t => t.Location == 1 && t.TextAlignment == CTTextAlignment.Natural)); }); }
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); }
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)AppKit.NSUnderlineStyle.Single; int solid = (int)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); }
public SizeF GetStringSize(string aString, string aFontName, float aFontSize, HorizontalAlignment aHorizontalAlignment, VerticalAlignment aVerticalAlignment) { var fontSize = aFontSize; float factor = 1; while (fontSize > 10) { fontSize /= 10; factor *= 10; } var vPath = new CGPath(); vPath.AddRect(new CGRect(0, 0, 512, 512)); vPath.CloseSubpath(); var vAttributedString = new NSMutableAttributedString(aString); var vAttributes = new CTStringAttributes(); // Load the font var vFont = NativeFontService.Instance.LoadFont(aFontName ?? _systemFontName, (float)fontSize); vAttributes.Font = vFont; // Set the horizontal alignment var vParagraphSettings = new CTParagraphStyleSettings(); switch (aHorizontalAlignment) { case HorizontalAlignment.Left: vParagraphSettings.Alignment = CTTextAlignment.Left; break; case HorizontalAlignment.Center: vParagraphSettings.Alignment = CTTextAlignment.Center; break; case HorizontalAlignment.Right: vParagraphSettings.Alignment = CTTextAlignment.Right; break; case HorizontalAlignment.Justified: vParagraphSettings.Alignment = CTTextAlignment.Justified; break; } var vParagraphStyle = new CTParagraphStyle(vParagraphSettings); vAttributes.ParagraphStyle = vParagraphStyle; // Set the attributes for the complete length of the string vAttributedString.SetAttributes(vAttributes, new NSRange(0, aString.Length)); // Create the framesetter with the attributed string. var vFrameSetter = new CTFramesetter(vAttributedString); var textBounds = GetTextSize(vFrameSetter, vPath); //Logger.Debug("{0} {1}",vSize,aString); vFrameSetter.Dispose(); vAttributedString.Dispose(); vParagraphStyle.Dispose(); //vFont.Dispose(); vPath.Dispose(); textBounds.Width *= factor; textBounds.Height *= factor; //vSize.Width = Math.Ceiling(vSize.Width); //vSize.Height = Math.Ceiling(vSize.Height); return(textBounds.Size); }
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)AppKit.NSUnderlineStyle.Single; int solid = (int)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)AppKit.NSUnderlineStyle.Single; int solid = (int)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); }
static void WriteParagraphStyle (CTParagraphStyle s) { WriteProperties (s); var ts = s.GetTabStops (); Console.WriteLine ("#\tTabStops: ({0})", ts.Length); foreach (var t in ts) { var o = t.GetOptions (); Console.WriteLine ("#\t\tLocation={0}; TextAlignment={1}; ColumnTerminators={2}", t.Location, t.TextAlignment, o != null ? o.ColumnTerminators.ToString() : "<none>"); } }
static void ParagraphStyleChecks () { var s = new CTParagraphStyle (null); Console.WriteLine ("# default CTParagraphStyle: "); WriteParagraphStyle (s); s = new CTParagraphStyle (new CTParagraphStyleSettings () { TabStops = new CTTextTab[0], MaximumLineHeight = 1.0f, }); Console.WriteLine ("# modified CTParagraphStyle: "); WriteParagraphStyle (s); if (s.MaximumLineHeight != 1.0f) throw new InvalidOperationException ("MaximumLineHeight property not set!"); if (s.GetTabStops ().Length != 0) throw new InvalidOperationException ("TabStops property not set!"); }
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)AppKit.NSUnderlineStyle.Single; int solid = (int)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)AppKit.NSUnderlineStyle.Single; int solid = (int)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)AppKit.NSUnderlineStyle.Single; int solid = (int)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; }
public SizeF GetStringSize( string value, string fontName, float fontSize, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) { float factor = 1; while (fontSize > 10) { fontSize /= 10; factor *= 10; } var path = new CGPath(); path.AddRect(new Drawing.RectangleF(0, 0, 512, 512)); path.CloseSubpath(); var attributedString = new NSMutableAttributedString(value); var attributes = new CTStringAttributes(); // Load the font var font = NativeFontService.Instance.LoadFont(fontName ?? SystemFontName, fontSize); attributes.Font = font; // Set the horizontal alignment var paragraphSettings = new CTParagraphStyleSettings(); switch (horizontalAlignment) { case HorizontalAlignment.Left: paragraphSettings.Alignment = CTTextAlignment.Left; break; case HorizontalAlignment.Center: paragraphSettings.Alignment = CTTextAlignment.Center; break; case HorizontalAlignment.Right: paragraphSettings.Alignment = CTTextAlignment.Right; break; case HorizontalAlignment.Justified: paragraphSettings.Alignment = CTTextAlignment.Justified; break; } var paragraphStyle = new CTParagraphStyle(paragraphSettings); attributes.ParagraphStyle = paragraphStyle; // Set the attributes for the complete length of the string attributedString.SetAttributes(attributes, new NSRange(0, value.Length)); // Create the frame setter with the attributed string. var frameSetter = new CTFramesetter(attributedString); var textBounds = GetTextSize(frameSetter, path); //Logger.Debug("{0} {1}",vSize,aString); frameSetter.Dispose(); attributedString.Dispose(); paragraphStyle.Dispose(); //font.Dispose(); path.Dispose(); textBounds.Width *= factor; textBounds.Height *= factor; //size.Width = Math.Ceiling(vSize.Width); //size.Height = Math.Ceiling(vSize.Height); return(textBounds.Size); }
void drawLabel(CGContext ctx, CGRect rect, nfloat yCoord, nfloat xCoord, CTTextAlignment alignment, UIColor color, string label, bool flipContext = true) { Console.WriteLine("Finish Draw code"); var attrString = new NSMutableAttributedString (label); var range = new NSRange (0, attrString.Length); var uiFont = UIFont.FromName("HelveticaNeue-Medium", range.Length > 1 ? 15 : 22); var font = new CTFont (uiFont.Name, uiFont.PointSize); //((uiFont.Name as NSString) as CFString, uiFont.pointSize, nil); var path = new CGPath (); var alignStyle = new CTParagraphStyle (new CTParagraphStyleSettings { Alignment = alignment }); var attributes = new CTStringAttributes { Font = font, ForegroundColor = color.CGColor, ParagraphStyle = alignStyle }; attrString.SetAttributes(attributes, new NSRange (0, attrString.Length)); var target = new CGSize (nfloat.MaxValue, nfloat.MaxValue); var fit = new NSRange (0, 0); var framesetter = new CTFramesetter (attrString); var frameSize = framesetter.SuggestFrameSize(range, null, target, out fit); var textRect = new CGRect (xCoord - (frameSize.Width / 2), yCoord - (frameSize.Height / 2), frameSize.Width, frameSize.Height); path.AddRect(textRect); var frame = framesetter.GetFrame(range, path, null); if (flipContext) { ctx.SaveState(); ctx.TranslateCTM(0, rect.Height); ctx.ScaleCTM(1, -1); frame.Draw(ctx); ctx.RestoreState(); } else { frame.Draw(ctx); } }