public static float MeasureStringWidth(CGContext context, string text, string fontName, float fontSize) { if (string.IsNullOrEmpty(text)) return 0; context.SaveState(); PointF pos = context.TextPosition; context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman); context.TextMatrix = CGAffineTransform.MakeScale(1.0f, -1.0f); //context.TranslateCTM(0, 20); context.ScaleCTM(1, -1); context.SetTextDrawingMode(CGTextDrawingMode.Invisible); context.ShowTextAtPoint(pos.X, pos.Y, text); PointF pos2 = context.TextPosition; context.RestoreState(); return pos2.X - pos.X; }
public static void DrawText(CGContext context, string text, string fontName, float fontSize, float translateHeight, float x, float y) { context.SaveState(); context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman); context.SetTextDrawingMode(CGTextDrawingMode.Fill); context.SetFillColor(new CGColor(1, 1)); context.SetStrokeColor(new CGColor(1.0f, 1.0f)); //context.AddRect(rectText); //context.Clip(); context.TextMatrix = CGAffineTransform.MakeScale(1.0f, -1.0f); context.TranslateCTM(0, translateHeight); context.ScaleCTM(1, -1); context.ShowTextAtPoint(x, y, text); context.RestoreState(); }