CATextLayer不会绘制NSAttributedString背景颜色

时间:2011-08-24 11:53:53

标签: objective-c cocoa

我有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;

前三个符号以黄色绘制,但背景不会改变。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我认为CATextLayer类使用CoreText API进行渲染。 CoreText API非常低级,仅支持NSAttributedString属性的子集。不幸的是,NSBackgroundColorAttributeName不是其中之一。您必须手动处理它(请参阅this SO entry)。