drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment [英] drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment

查看:129
本文介绍了drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我可以'弄清楚为什么 drawInRect:withAttributes 无效。当 drawInRect:withFont:lineBreakMode:alignment 消息被发送时,代码显示正常,但是当 drawInRect:withAttributes 时,该代码不起作用发送。

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

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

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.

(请注意,下面使用的 bs.name 是NSString对象)

(note that bs.name used below is an NSString object)

        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.

推荐答案

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

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

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

这篇关于drawInRect:withAttributes vs drawInRect:withFont:lineBreakMode:alignment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆