void UpdateTextOnControl() { if (Control == null) { return; } //define paragraph-style var style = new NSMutableParagraphStyle() { Alignment = UITextAlignment.Justified, FirstLineHeadIndent = 0.001f, }; //define attributes that use both paragraph-style, and font-style var uiAttr = new UIStringAttributes() { ParagraphStyle = style, BaselineOffset = 0, Font = Control.Font }; //define frame to ensure justify alignment is applied Control.Frame = new RectangleF(0, 0, (float)Element.Width, (float)Element.Height); //set new text with ui-style-attributes to native control (UILabel) var stringToJustify = Control.Text ?? string.Empty; var attributedString = new Foundation.NSAttributedString(stringToJustify, uiAttr.Dictionary); Control.AttributedText = attributedString; Control.Lines = 0; }
private static CGSize MeasureTextSize2(string text, CoreText.CTFont font) { // https://docs.microsoft.com/en-us/dotnet/api/foundation.nsattributedstring?view=xamarin-ios-sdk-12 var attributedString = new Foundation.NSAttributedString(text, new CoreText.CTStringAttributes() { Font = font }); return(attributedString.Size); }
private nfloat textViewheightForRow() { UITextView calculationView = this._textViewReference; nfloat textViewWidth = calculationView.Frame.Size.Width; if (calculationView.AttributedText == null) { calculationView = new UITextView(); Foundation.NSAttributedString astring = new Foundation.NSAttributedString("This is some test text"); calculationView.AttributedText = astring; textViewWidth = 320; } CoreGraphics.CGSize size = calculationView.SizeThatFits(new CoreGraphics.CGSize(textViewWidth, float.MaxValue)); return(size.Height); }
private nfloat textViewheightForRow() { UITextView calculationView = this._textViewReference; nfloat textViewWidth = calculationView.Frame.Size.Width; if (calculationView.AttributedText == null) { calculationView = new UITextView (); Foundation.NSAttributedString astring = new Foundation.NSAttributedString ("This is some test text"); calculationView.AttributedText = astring; textViewWidth = 320; } CoreGraphics.CGSize size = calculationView.SizeThatFits (new CoreGraphics.CGSize(textViewWidth, float.MaxValue)); return size.Height; }
// 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); }