I have CATextLayer and want to set background color to part of the string. But setting background color to attributed string (NSBackgroundColorAttributeName) doesn't have any effect. Other attributes, such as foreground color, are applied correctly.
NSMutableAttributedString *str = [[[NSMutableAttributedString alloc] initWithString:@"Some Text"] autorelease];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor yell开发者_开发知识库owColor], NSForegroundColorAttributeName,
[NSColor redColor], NSBackgroundColorAttributeName, nil];
[str setAttributes:attributes range:NSMakeRange(0, 3)];
textLayer.string = str;
First three symbols are drawn in yellow color, but background won't change. Any ideas?
I think that the CATextLayer
class uses the CoreText
API for its rendering. The CoreText
API are very low-level and only support a subset of the NSAttributedString
attributes. Unfortunately, the NSBackgroundColorAttributeName
is not one of those. You have to deal with it manually (see this SO entry).
精彩评论