drawInRect:withAttributes与drawInRect:withFont:lineBreakMode:alignment的区别

30

我正在开发我的应用的新版本,试图替换已弃用的消息,但是无法通过此问题。

我不明白为什么drawInRect:withAttributes不能工作。当发送drawInRect:withFont:lineBreakMode:alignment消息时,代码可以正确显示,但是发送drawInRect:withAttributes时却无法正常工作。

我正在使用相同的矩形和字体以及我认为是相同的文本样式。 常量只是将矩形定位在图像正下方,但我对两种调用都使用了相同的矩形,所以我确定矩形是正确的。

(下面注意到bs.name 是一个NSString对象)

        CGRect textRect = CGRectMake(fCol*kRVCiPadAlbumColumnWidth,
                                     kRVCiPadAlbumColumnWidth-kRVCiPadTextLabelYOffset,
                                     kRVCiPadAlbumColumnWidth,
                                     kRVCiPadTextLabelHeight);
        NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        textStyle.lineBreakMode = NSLineBreakByWordWrapping;
        textStyle.alignment = NSTextAlignmentCenter;
        UIFont *textFont = [UIFont systemFontOfSize:16];

使用上面的变量,这不起作用(屏幕上没有任何绘图)

        [bs.name drawInRect:textRect
             withAttributes:@{NSFontAttributeName:textFont,
                              NSParagraphStyleAttributeName:textStyle}];

使用上面相同的变量,这确实有效(字符串能够正确地显示在屏幕上)

        [bs.name drawInRect:textRect
                   withFont:textFont
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment:NSTextAlignmentCenter];

希望能得到任何帮助,谢谢。


你说drawInRect:withAttributes:不起作用是什么意思? - Arek Holko
非常好的问题。抱歉我没有详细说明。在使用drawInRect:withFont:lineBreakMode:alignment方法时,包含在bs.name中的字符串会被正确地绘制到屏幕上,但是在使用drawInRect:withAttributes方法时(什么都没有被绘制)。 - Ross Pirtle
2个回答

37
为了设置文本的颜色,您需要在属性中传递NSForegroundColorAttributeName作为附加参数。
NSDictionary *dictionary = @{ NSFontAttributeName: self.font,
                              NSParagraphStyleAttributeName: paragraphStyle,
                              NSForegroundColorAttributeName: self.textColor};

30
我已经使用 drawRect: 创建了一个 UIView,其中只包含您提供的代码。
- (void)drawRect:(CGRect)frame
{
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
    textStyle.alignment = NSTextAlignmentCenter;
    UIFont *textFont = [UIFont systemFontOfSize:16];

    NSString *text = @"Lorem ipsum";

    // iOS 7 way
    [text drawInRect:frame withAttributes:@{NSFontAttributeName:textFont, NSParagraphStyleAttributeName:textStyle}];

    // pre iOS 7 way
    CGFloat margin = 16;
    CGRect bottomFrame = CGRectMake(0, margin, frame.size.width, frame.size.height - margin);
    [text drawInRect:bottomFrame withFont:textFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}

我没有看到这两种方法的输出之间有任何区别。也许问题在你的代码的其他地方?


感谢您的工作,提供这个答案。我的代码也是在UIView的drawRect:方法中执行的。我同意它应该放在其他地方,但如果我注释掉其中一个,一个可以工作而另一个不行。也许需要重新计算矩形,出于某种原因,在iOS 7上与以往不同(只是为了提供信息,iOS 7的方法是drawInRect:withAttributes)。我会再仔细调整一下矩形。再次感谢。 - Ross Pirtle
确保传递给两个方法的 CGRect 是相同的。您还可以尝试 1)更改文本颜色,2)在矩形上绘制背景,以确保其定位在所需位置。这些步骤相当简单,但也许它们能帮助找到问题所在。 - Arek Holko
我在完全相同的方法中使用完全相同的代码(不是两种方法)。如果您将旧的drawRect:替换为新的drawRect:在完全相同的位置使用完全相同的矩形,则一个有效,另一个无效。我仍然感到困惑。如果我找到任何东西,我会发布。 - Ross Pirtle
3
好的,我已经弄清楚了,这有点显而易见(大多数情况下可能是这样)。感谢您的建议!我之前使用的是:[[UIColor lightGrayColor] set]; 来设置文本颜色,在早期版本的 drawInRect: 中可以使用该方法。在新版本中,您需要将文本颜色设置为一个属性。所以它本来是有效的,只是在黑色背景上绘制黑色文本。我无法发布答案,因为我是新用户。 - Ross Pirtle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接