首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >drawInRect:withAttributes vs drawInRect:withFont:line根模式:对齐

drawInRect:withAttributes vs drawInRect:withFont:line根模式:对齐
EN

Stack Overflow用户
提问于 2013-10-09 14:08:28
回答 1查看 30.9K关注 0票数 30

我正在开发我的应用程序的一个新版本,并试图替换不受欢迎的消息,但无法通过这个版本。

我不明白为什么drawInRect:withAttributes不工作。当发送drawInRect:withFont:lineBreakMode:alignment消息时,代码将正确显示,但在发送drawInRect:withAttributes时不起作用。

我用的是同样的字体和字体,我相信这是相同的文字风格。常量只是将rect定位在图像下面,但是对于两个调用,我使用的是相同的rect,所以我确信矩形是正确的。

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

代码语言:javascript
运行
复制
        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];

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

代码语言:javascript
运行
复制
        [bs.name drawInRect:textRect
             withAttributes:@{NSFontAttributeName:textFont,
                              NSParagraphStyleAttributeName:textStyle}];

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

代码语言:javascript
运行
复制
        [bs.name drawInRect:textRect
                   withFont:textFont
              lineBreakMode:NSLineBreakByWordWrapping
                  alignment:NSTextAlignmentCenter];

任何帮助都是很好的。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-10-10 10:18:10

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

代码语言:javascript
运行
复制
NSDictionary *dictionary = @{ NSFontAttributeName: self.font,
                              NSParagraphStyleAttributeName: paragraphStyle,
                              NSForegroundColorAttributeName: self.textColor};
票数 37
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19274408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档