【发布时间】:2013-08-06 05:06:06
【问题描述】:
我正在尝试绘制 UITextView 的背景线, 这是我用来绘制这些线条的代码
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, self.horizontalLineColor.CGColor);
CGContextSetLineWidth(context, kDashedBorderWidth);
CGContextSetLineDash(context, 0, kDashedLinesLength, kDashedCount) ;
CGFloat baseOffset = 9.0f + self.font.descender;
CGFloat boundsX = self.bounds.origin.x;
CGFloat boundsWidth = self.bounds.size.width;
NSInteger firstVisibleLine = MAX(1, (self.contentOffset.y / self.font.lineHeight));
NSInteger lastVisibleLine = ceilf((self.contentOffset.y + self.bounds.size.height) / self.font.lineHeight);
for (NSInteger line = firstVisibleLine; line <= lastVisibleLine; ++line)
{
CGFloat linePointY = (baseOffset + (self.font.lineHeight * (float)line));
CGContextMoveToPoint(context, boundsX, linePointY);
CGContextAddLineToPoint(context, boundsWidth, linePointY);
}
CGContextClosePath(context);
CGContextStrokePath(context);
结果如图所示
它似乎正确地获取了第一行,但以下行与文本不对齐。
我可能在这里错过了什么?与本地化设置有什么关系吗?
【问题讨论】:
-
看起来您的行高计算已关闭。它最初是对齐的,因为第一行对齐正确,但是由于文本的行高大于虚线的行高,它最终变得越来越不合规。我将首先查看您对线高的计算并进行调整。
-
我同意,
self.font.lineHeight似乎没有提供精确的高度值。 -
显然 UITextView 的内部渲染没有使用
lineHeight属性来布置线条。不知道内部是如何工作的,就不可能准确地画出线条。 -
您好,请问您是如何从
baseOffset推导出9.0f的?谢谢。
标签: ios uitextview