ios - iOS7中的内衬UITextView

标签 ios ipad uitextview ios7 uifont

我有一个 UITextView(自定义控件 DALInedTextView)的子类,我在其中绘制线条文本。它在 iOS5 和 iOS6 上完美运行,但在 iOS7 上失败(文本与行不匹配)。

   - (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0f);

    if (self.horizontalLineColor)
    {
        CGContextBeginPath(context);
        CGContextSetStrokeColorWithColor(context, self.horizontalLineColor.CGColor);

        // Create un-mutated floats outside of the for loop.
        // Reduces memory access.
        CGFloat baseOffset = 7.0f + self.font.descender;
        CGFloat screenScale = [UIScreen mainScreen].scale;
        CGFloat boundsX = self.bounds.origin.x;
        CGFloat boundsWidth = self.bounds.size.width;

        // Only draw lines that are visible on the screen.
        // (As opposed to throughout the entire view's contents)
        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 * line));
            // Rounding the point to the nearest pixel.
            // Greatly reduces drawing time.
            CGFloat roundedLinePointY = roundf(linePointY * screenScale) / screenScale;
            CGContextMoveToPoint(context, boundsX, roundedLinePointY);
            CGContextAddLineToPoint(context, boundsWidth, roundedLinePointY);
        }
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }

    if (self.verticalLineColor)
    {
        CGContextBeginPath(context);
        CGContextSetStrokeColorWithColor(context, self.verticalLineColor.CGColor);
        CGContextMoveToPoint(context, -1.0f, self.contentOffset.y);
        CGContextAddLineToPoint(context, -1.0f, self.contentOffset.y + self.bounds.size.height);
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }
}

我知道这与 UIFont 指标相关。也许有人可以帮助我?我已将contentSize更改为intrinsicContentSize,但它不起作用。

如果我使用 systemFontOfSize 它可以完美工作,但是使用 fontWithName 它会失败。

enter image description here

最佳答案

我已经为这个奇怪的问题苦苦挣扎了一段时间,但最终偶然发现了一个合法的解决方案:

textView.layoutManager.usesFontLeading = NO;

这使得 UITextView 渲染文本(几乎)与 UILabel 相同。

关于ios - iOS7中的内衬UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855438/

相关文章:

ios - 每个约束项都必须是 View 或布局指南

ios - 再次打开我的应用程序后,如何保存对象数组并使用相同的值重复使用。 # swift

ios - 如何使函数在 AFHTTPRequestOperation 完成后返回

ios - 将 mp4 视频保存到设备相机胶卷

ios - App Store 提交,iPhone 与 iPad 尺寸

ios - 将组件放置在 UITextView 下方并调整大小以适合

iphone - UITextField 和 UIPickerView

ios - 设置最大行可编辑 TextView - Swift/iOS

ios - Cordova.js 错误

javascript - HTML5 视频上的关闭按钮 (iPad)