更改 UILabel 的第一行颜色
Change first line color of UILabel
我正在尝试更改 UILabels
第一行的颜色。由于某种原因,它不起作用。这是我的代码:
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[label.text rangeOfString:@"\n"];];
[label setAttributedText: text];
我没有看到第一行有任何变化。
问题就在这里
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[label.text rangeOfString:@"\n"]];
它只会着色 \n
您需要从 0
到 \n
开头的范围
编辑:您可以试试这段代码(未经测试但应该可以)
NSRange rangeOfNewLine = [label.text rangeOfString:@"\n"];
NSRange newRange = NSMakeRange(0, rangeOfNewLine.location);
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:newRange];
我正在尝试更改 UILabels
第一行的颜色。由于某种原因,它不起作用。这是我的代码:
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[label.text rangeOfString:@"\n"];];
[label setAttributedText: text];
我没有看到第一行有任何变化。
问题就在这里
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[label.text rangeOfString:@"\n"]];
它只会着色 \n
您需要从 0
到 \n
编辑:您可以试试这段代码(未经测试但应该可以)
NSRange rangeOfNewLine = [label.text rangeOfString:@"\n"];
NSRange newRange = NSMakeRange(0, rangeOfNewLine.location);
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:newRange];