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); }
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); }