ios drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19274408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:14:41  来源:igfitidea点击:

drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

iosobjective-cnsstringdrawinrect

提问by Ross Pirtle

I'm working on a new version of my app and am attempting to replace deprecated messages, but am not able to get past this one.

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

I can't figure out why drawInRect:withAttributesis not working. The code displays properly when drawInRect:withFont:lineBreakMode:alignmentmessage is sent, but does not work when drawInRect:withAttributesis sent.

我无法弄清楚为什么drawInRect:withAttributes不起作用。该代码在drawInRect:withFont:lineBreakMode:alignment发送消息时正确显示,但在drawInRect:withAttributes发送时不起作用。

I'm using the same rect and font and I what I believe is the same text style. The constants are just positioning the rect just below an image, but I'm using the same rect for both calls, so I'm certain the rectangle is correct.

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

(note that bs.nameused below is an NSString object)

(注意下面使用的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];

This doesn't work (nothing is drawn on the screen) using the variables from above

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

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

This Does work (the string is drawn properly on the screen) using the same variables from above

这确实有效(字符串在屏幕上正确绘制)使用与上面相同的变量

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

Any assistance would be great. Thanks.

任何帮助都会很棒。谢谢。

回答by Hiren

To set the color of text you need to pass the NSForegroundColorAttributeNamein the attribute as the additional parameter.

要设置文本的颜色,您需要NSForegroundColorAttributeName将属性作为附加参数传递。

NSDictionary *dictionary = @{ NSFontAttributeName: self.font,
                              NSParagraphStyleAttributeName: paragraphStyle,
                              NSForegroundColorAttributeName: self.textColor};

回答by Arek Holko

I've made a UIViewwith drawRect:containing only the code you provided

我做了一个UIViewdrawRect:仅含您所提供的代码

- (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];
}

I don't see any difference between the outputs of these two methods. Maybe the problem is somewhere else in your code?

我看不出这两种方法的输出有什么区别。也许问题出在您的代码中的其他地方?