我想在我的自定义视图中绘制带有背景颜色的文本,但我发现如果我将 NSBackgroundColorAttributeName 属性添加到我的 NSAttributedString ,它不会出现。当我删除背景颜色属性时,它会正常绘制:
-(void) drawRect : (CGRect)rect
{
NSAttributedString *test = [[NSAttributedString alloc]initWithString:@"test"
attributes: @{NSForegroundColorAttributedName:[UIColor whiteColor],
NSBackgroundColorAttributeName:[UIColor blackColor]}];
[test drawAtPoint:CGPointMake(0,0)];
}
但是,如果我在自定义视图上添加标签并修改其属性文本,它将起作用:
NSMutableAttributedString *attributedText = [self.testLabel.attributedText mutableCopy];
NSString *str = [attributedText string];
NSRange r = [str rangeOfString:str];
[attributedText addAttributes: @{NSForegroundColorAttributedName:[UIColor whiteColor],
NSBackgroundColorAttributeName:[UIColor blackColor]} range:r];
self.testLabel.attributedText = arrtibutedText;
这是为什么 ?