public static TextMetrics GlobalMeasureText(string text, Font font) { if (string.IsNullOrEmpty(text)) { return(new TextMetrics()); } if (font == null) { throw new ArgumentNullException("font"); } using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }.Dictionary, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { nfloat asc, desc, lead; var len = l.GetTypographicBounds(out asc, out desc, out lead); return(new TextMetrics { Width = len, Ascent = asc, Descent = desc, }); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty(text)) { return; } if (font == null) { throw new ArgumentNullException("font"); } SetBrush(brush); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { context.SaveState(); context.TranslateCTM((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw(context); context.RestoreState(); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty(text)) { return; } if (font == null) { throw new ArgumentNullException("font"); } SetBrush(brush); using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { nfloat asc, desc, lead; var len = l.GetTypographicBounds(out asc, out desc, out lead); var pt = frame.TopLeft; switch (alignment) { case TextAlignment.Left: pt.X = frame.X; break; case TextAlignment.Center: pt.X = frame.X + (frame.Width - len) / 2; break; case TextAlignment.Right: pt.X = frame.Right - len; break; } context.SaveState(); context.TranslateCTM((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw(context); context.RestoreState(); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty(text)) { return; } if (font == null) { throw new ArgumentNullException("font"); } SetBrush(brush); // string fontName = font.Name; // context.SelectFont (font.Name, (nfloat)font.Size, CGTextEncoding.MacRoman); // // context.ShowTextAtPoint ((nfloat)frame.X, (nfloat)frame.Y, text); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { nfloat asc, desc, lead; var width = l.GetTypographicBounds(out asc, out desc, out lead); context.SaveState(); context.TranslateCTM((nfloat)(pt.X - width / 2), (nfloat)(pt.Y + desc)); context.TextPosition = CGPoint.Empty; l.Draw(context); context.RestoreState(); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); // string fontName = font.Name; // context.SelectFont (font.Name, (nfloat)font.Size, CGTextEncoding.MacRoman); // // context.ShowTextAtPoint ((nfloat)frame.X, (nfloat)frame.Y, text); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { nfloat asc, desc, lead; var width = l.GetTypographicBounds (out asc, out desc, out lead); context.SaveState (); context.TranslateCTM ((nfloat)(pt.X - width / 2), (nfloat)(pt.Y + desc)); context.TextPosition = CGPoint.Empty; l.Draw (context); context.RestoreState (); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { context.SaveState (); context.TranslateCTM ((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw (context); context.RestoreState (); } } }
public static TextMetrics GlobalMeasureText (string text, Font font) { if (string.IsNullOrEmpty(text)) return new TextMetrics (); if (font == null) throw new ArgumentNullException("font"); using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { nfloat asc, desc, lead; var len = l.GetTypographicBounds (out asc, out desc, out lead); return new TextMetrics { Width = len, Ascent = asc, Descent = desc, }; } } }
public void DrawText (string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, StrokeColor = pen != null ? pen.Color.GetCGColor () : null, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { nfloat asc, desc, lead; var len = l.GetTypographicBounds (out asc, out desc, out lead); var pt = frame.TopLeft; switch (alignment) { case TextAlignment.Left: pt.X = frame.X; break; case TextAlignment.Center: pt.X = frame.X + (frame.Width - len) / 2; break; case TextAlignment.Right: pt.X = frame.Right - len; break; } context.SaveState (); context.TranslateCTM ((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw (context); context.RestoreState (); } } }