簡體   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