CATextLayer不会绘制NSAttributedString背景色 [英] CATextLayer doesn't draw NSAttributedString background color
问题描述
我有CATextLayer,想将背景颜色设置为字符串的一部分。但是,将背景颜色设置为属性字符串(NSBackgroundColorAttributeName)无效。正确应用了其他属性,例如前景色。
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 yellowColor], 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?
推荐答案
我认为 CATextLayer
类使用 CoreText
API进行渲染。 CoreText
API是非常底层的,仅支持 NSAttributedString
属性的子集。不幸的是, NSBackgroundColorAttributeName
并不是其中之一。您必须手动处理它(请参见此SO条目)。
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).
这篇关于CATextLayer不会绘制NSAttributedString背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!