// using the attributes of SKPaint paint parameter: //Typeface, //TextSize, //TextAlign(Center|Left), //Style(Fill|Stroke), //Color, //StrokeWidth(if Style=Stroke) public static CGSize DrawTextS(CGContext canvas, string text, SKPoint point, SKPaint paint) { // BASIC TEXT DRAWING IN CGContext: //canvas.SetTextDrawingMode(toCGTextDrawingMode(paint.Style)); //canvas.SetFillColor(CGUtil.toCGColor(paint.Color)); //canvas.SetStrokeColor(CGUtil.toCGColor(paint.Color)); //canvas.SetLineWidth(paint.StrokeWidth); //canvas.SelectFont("Helvetica", paint.TextSize, CGTextEncoding.MacRoman); //// upsidedown text: https://stackoverflow.com/questions/44122778/ios-swift-core-graphics-string-is-upside-down //canvas.SaveState(); //canvas.TranslateCTM(center.X, center.Y); // text should be centered, but isn't //canvas.ScaleCTM(1, -1); //canvas.ShowTextAtPoint(0, 0, text); //canvas.RestoreState(); // TEXT DRAWING IN CoreText to compute text size: // https://docs.microsoft.com/en-us/dotnet/api/foundation.nsattributedstring?view=xamarin-ios-sdk-12 var typeface = (paint.Typeface == null) ? "Helvetica" : paint.Typeface.FamilyName; var attributedString = new Foundation.NSAttributedString(text, new CoreText.CTStringAttributes() { ForegroundColor = Color(paint.Color), // ForegroundColor is used (only?) if paint.Style = Fill, and overrides paint.Color Font = new CoreText.CTFont(typeface, paint.TextSize) }); var textLine = new CoreText.CTLine(attributedString); CGSize textSize = attributedString.Size; // maybe we can extract the true baseline origin and return a Rect instead of a Size? bool centered = paint.TextAlign == SKTextAlign.Center; CGPoint textStart = (centered) ? new CGPoint(point.X - textSize.Width / 2.0f, point.Y) : Point(point); canvas.SetTextDrawingMode(CG.TextDrawingMode(paint.Style)); canvas.SetStrokeColor(CG.Color(paint.Color)); // paint.Color is used (only?) if paint.Style = Stroke canvas.SetLineWidth(paint.StrokeWidth); // upsidedown text: https://stackoverflow.com/questions/44122778/ios-swift-core-graphics-string-is-upside-down canvas.SaveState(); canvas.TranslateCTM(textStart.X, textStart.Y); canvas.ScaleCTM(1, -1); canvas.TextPosition = new CGPoint(0, 0); // because of the translation textLine.Draw(canvas); canvas.RestoreState(); return(textSize); }