开发者

CATextLayer doesn't draw NSAttributedString background color

开发者 https://www.devze.com 2023-03-30 15:34 出处:网络
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

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 yell开发者_开发知识库owColor], 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消