2011-08-24 129 views
4

我有CATextLayer並且想要設置背景顏色爲部分字符串。但是將背景顏色設置爲屬性字符串(NSBackgroundColorAttributeName)沒有任何作用。其他屬性(如前景色)正確應用。CATextLayer不繪製NSAttributedString背景顏色

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; 

前三個符號用黃色繪製,但背景不會改變。有任何想法嗎?

回答

3

我認爲CATextLayer類使用CoreText API進行渲染。 CoreText API非常低級,僅支持NSAttributedString屬性的子集。不幸的是,NSBackgroundColorAttributeName不是其中之一。您必須手動處理(請參閱this SO entry)。