1

我有一个字符串,假设"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 返回了3HOWEVER!当我将代码更改为:

[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)];

(现在整个消息“,你好”是白色的,这是唯一的变化) glyphCount 返回1 为什么会这样以及如何避免它?这看起来相当奇怪。

4

0 回答 0