繁体   English   中英

在iOS 6中使用CoreText设置NSAttributedString的颜色不起作用

[英]Using CoreText To Set Color of NSAttributedString in iOS 6 Not Working

我有以下代码:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_textView.text];
NSRange totalRange = NSMakeRange(0, [attributedString length]);
[attributedString addAttribute:(id)kCTForegroundColorAttributeName
                         value:(id)[UIColor yellowColor].CGColor
                         range:totalRange];

当在iOS 6上运行时,总是导致该字符串显示为红色。

有趣的是,CoreText可用于指定字体,如下所示:

[attributedString addAttribute:(id)kCTFontAttributeName
                         value:[UIFont boldSystemFontOfSize:20]
                         range:specialFontRange];

通过这种方式设置颜色效果很好:

[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor yellowColor]
                         range:totalRange];

但是出于兼容性原因,我必须使用CoreText。 关于如何使用CoreText在iOS 6中指定颜色的任何想法?

这实际上取决于您如何绘制文本。 如果您使用-(void)drawInRect:(CGRect)rect; -(void)drawAtPoint:(CGPoint)point; (仅在iOS6或更高版本中可用),则必须使用NSAttributedString Additions Reference常量部分中指定的常量 例如,要设置文本颜色,必须使用键NSForegroundColorAttributeName

如果您使用Core Graphics进行绘制,则需要遵循Core Text String Attributes Reference中指定的常量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM