0

我有一个 UITextView (自定义控件 DLinedTextView)的子类,我在其中绘制横线文本。它在 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 会失败。

在此处输入图像描述

4

3 回答 3

1

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

textView.layoutManager.usesFontLeading = NO;

这使得UITextView渲染文本(几乎)与UILabel.

于 2013-12-12T13:21:36.557 回答
0

是的,我认为您正在使用自定义控件DALinedTextView

我也面临着你在iOS7中所说的问题。即使您无法在iOS7.

首先我使用该控件,现在我离开它并使用内置的UITextView. :D

或许font size,您需要更改您的.text margintext paddingtextView

于 2013-10-01T09:09:54.573 回答
0

检查这个链接。它使用在 ios7 中使用的 NSLayoutManagerDelegate。对于 iOS 7,styleString 方法不再有效。你必须使用 NSLayoutManagerDelegate,它很容易使用。

txtViewNote.layoutManager.delegate = self;
txtViewNote.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textView_bg_lines"]];

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 20.5; // For really wide spacing
}
于 2013-10-25T12:00:11.393 回答