NSMutableAttributedString 诡异的问题

NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。

这也是手动实现UIWeb布局的重要手段,其中一个诡异问题。


废话不多说直接上代码把。

   NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"设置背景位红色。(红色)"];
   [text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:(NSRange){9,2}];
    
   Label.attributedText=text;

希望的效果



但是运用以后始终没有任何效果。



使用下面的代码又可以正常运行

   NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"设置背景位红色。(红色)"];
   [text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:(NSRange){0,2}];
   [text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:(NSRange){9,2}];
    
   Label.attributedText=text;

  
 

最后设置背景颜色只能使用下面代码。我想这个应该是IOS的一个BUG把。

   NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"设置背景位红色。(红色)"];
   [text addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:(NSRange){0,2}];
   [text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:(NSRange){9,2}];
    
   Label.attributedText=text;


你可能感兴趣的:(NSMutableAttributedString 诡异的问题)