1

I have a string, let's say "Hello" I need to append in front and at the end "," so the string looks like this: ",Hello," now I want the text to be white and the "," in different color. So I do something like this:

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

later I do all the stuff with the CTFrameSetterRed, CGContextRef

CTLineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines, index);
CFArrayRef glyphRuns = CTLineGetGlyphRuns(line);
CFIndex glyphCount = CFArrayGetCount(glyphRuns);

And in this ",Hello," example the glyphCount returns 3 HOWEVER! when I change the code to:

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

(now the whole message ",Hello," is white, thats the only change) the glyphCount returns 1 Why is it so and how to avoid it? This looks rather strange.

L3M0L
  • 429
  • 8
  • 23

0 Answers0