Objective c CtlineGetGlyph使用NSAttributedString运行奇怪的行为
我有一个字符串,比如说,Objective c CtlineGetGlyph使用NSAttributedString运行奇怪的行为,objective-c,nsattributedstring,glyphrun,Objective C,Nsattributedstring,Glyphrun,我有一个字符串,比如说,“Hello”我需要在前面和后面添加“”,“所以字符串看起来像这样:”,Hello,“现在我希望文本是白色的,而“,”是不同颜色的。所以我做了这样的事情: [attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(range.location+1, range.length-
“Hello”
我需要在前面和后面添加“”,“
所以字符串看起来像这样:”,Hello,“
现在我希望文本是白色的,而“,”是不同颜色的。所以我做了这样的事情:
[attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(range.location+1, range.length-1)];
[attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(range.location, 1)];
[attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(range.length-1, 1)];
后来我用CTFrameSetterRed
,CGContextRef
CTLineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines, index);
CFArrayRef glyphRuns = CTLineGetGlyphRuns(line);
CFIndex glyphCount = CFArrayGetCount(glyphRuns);
在这个“,Hello,”
示例中,glyphCount返回3
!当我将代码更改为:
[attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(range.location+1, range.length-1)];
[attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(range.location, 1)];
[attributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(range.length-1, 1)];
(现在整个消息“Hello”是白色的,这是唯一的更改)glyphCount返回1
为什么会这样?如何避免?这看起来很奇怪