Ios 在自定义视图的drawRect函数中设置NSAttributedString的背景色
我想在自定义视图中使用背景色绘制文本,但我发现如果 我将NSBackgroundColorAttributeName属性添加到NSAttributeString中,它不会出现。 当我删除“背景色”属性时,它会正常绘制:Ios 在自定义视图的drawRect函数中设置NSAttributedString的背景色,ios,background-color,nsattributedstring,custom-view,Ios,Background Color,Nsattributedstring,Custom View,我想在自定义视图中使用背景色绘制文本,但我发现如果 我将NSBackgroundColorAttributeName属性添加到NSAttributeString中,它不会出现。 当我删除“背景色”属性时,它会正常绘制: -(void) drawRect : (CGRect)rect { NSAttributedString *test = [[NSAttributedString alloc]initWithString:@"test" att
-(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;
为什么呢