[英]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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.