我想在自定义视图中使用背景颜色绘制文本,但我发现如果 我将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;
为什么会这样?