4

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?

inkvisitor
  • 43
  • 1
  • 3

1 Answers1

3

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).

Community
  • 1
  • 1
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81