[英]How do I get line heights of UITextView when using UIStringAttributes in Xamarin Forms iOS?
I have a problem with setting UIStringAttributes.Font
.我在设置UIStringAttributes.Font
时遇到问题。
Before setting it, the system defaults to Helvetica 12pt , with a line height of 13.8.在设置之前,系统默认为Helvetica 12pt ,行高为 13.8。
This line height seems accurate as shown by the image here:此行高似乎准确,如下图所示:
However if I set the UIStringAttributes.Font
property with Arial 17pt , the line height of 18.99 seems inaccurate as shown here:但是,如果我使用Arial 17pt设置UIStringAttributes.Font
属性,则 18.99 的行高似乎不准确,如下所示:
My code is as follows:我的代码如下:
void SetAttributes(string text)
{
NSMutableAttributedString astr = new NSMutableAttributedString();
UIStringAttributes attrs;
NSAttributedString str;
CheckBox cb;
string[] lines;
string line;
lines = text.Split("\n", StringSplitOptions.None);
for (int i = 0; i < lines.Count; i++)
{
cb = _checks[i];
line = lines[i];
if (i + 1 < _checks.Count)
line += "\n";
attrs = new UIStringAttributes();
attrs.ForegroundColor = cb.Color;
////////////////////////////////
//COMMENTING THIS LINE OUT ALLOWS A CORRECT
//LINEHEIGHT TO BE GIVEN IN DRAW METHOD
attrs.Font = Control.Font;
////////////////////////////////
str = new NSAttributedString(line, attrs);
astr.Append(str);
}
Control.AttributedText = astr;
}
public override void Draw(CGRect rect)
{
double baseline = editor.Padding.Top + Control.Font.Ascender;
double movingBaseline = baseline + BOTTOM_MARGIN;
double scrollY = Control.ContentOffset.Y;
double _lineHeight = Control.Font.LineHeight;
//get graphics context
using (CGContext g = UIGraphics.GetCurrentContext())
{
for (int i = 0; i < _lines.Count; i++)
{
CGPath path = new CGPath();
//set up drawing attributes
g.SetLineWidth(0.5f);
UIColor.Black.SetStroke();
//add lines to the touch points
path.AddLines(new CGPoint[] { new CGPoint(0, movingBaseline - scrollY),
new CGPoint(300, movingBaseline - scrollY) });
//add geometry to graphics context and draw it
g.AddPath(path);
g.DrawPath(CGPathDrawingMode.Stroke);
//tying this to the control.baseline hopefully should help
//to avoid accumulating rounding errors
movingBaseline = baseline + BOTTOM_MARGIN + (_lineHeight * (double)(i + 1));
}
}
base.Draw(rect);
}
I've tried:我试过了:
NSString(text).StringSize
-> gives height of 19 which is wrong NSString(text).StringSize
-> 给出 19 的高度,这是错误的ParagraphStyle.LineHeight
in UIStringAttributes
so I can control the height, but unfortunately it is not settable because Xamarin hasn't implemented a setter在UIStringAttributes
中设置ParagraphStyle.LineHeight
以便我可以控制高度,但不幸的是它不可设置,因为 Xamarin 尚未实现设置器Any ideas?有任何想法吗?
I believe I may have solved it, or at least have a workaround with:我相信我可能已经解决了它,或者至少有一个解决方法:
NSMutableParagraphStyle para = new NSMutableParagraphStyle();
para.LineSpacing = LINESPACING;
attrs.ParagraphStyle = para;
Setting the Font on the attributes was likely adding a default linespacing which I had no visibility of.在属性上设置字体可能会添加一个我看不到的默认行距。 If I explicitly set it myself, at least I know what value is being used and can calculate the line height as:如果我自己明确设置它,至少我知道正在使用什么值并且可以将行高计算为:
lineHeight = Control.Font.LineHeight + LINESPACING
This seems to work for now.这似乎暂时有效。 Will report back if it starts misbehaving again.如果它再次开始行为不端,将报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.