我有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;
前三个符号以黄色绘制,但背景不会改变.有任何想法吗?
解决方法
我认为CATextLayer类使用CoreText API进行渲染. CoreText API非常低级,仅支持NSAttributedString属性的子集.不幸的是,NSBackgroundColorAttributeName不是其中之一.你必须手动处理它(见
this SO entry).