我有CATextLayer并希望将背景颜色设置为字符串的一部分。但是将背景颜色设置为属性字符串(NSBackgroundColorAttributeName)没有任何效果。其他属性(如前景色)也会正确应用。
NSMutableAttributedString *str = [[[NSMutableAttributedString alloc] initWithString:@"Some Text"] autorelease];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor yellowColor], NSForegroundColorAttributeName,
[NSColor redColor], NSBackgroundColorAttributeName, nil];
[str setAttributes:attributes range:NSMakeRange(0, 3)];
textLayer.string = str;
前三个符号以黄色绘制,但背景不会改变。有什么想法吗?
答案 0 :(得分:3)
我认为CATextLayer
类使用CoreText
API进行渲染。 CoreText
API非常低级,仅支持NSAttributedString
属性的子集。不幸的是,NSBackgroundColorAttributeName
不是其中之一。您必须手动处理它(请参阅this SO entry)。