2016-08-24 95 views
0

我有兩個標籤,並使用一個歸屬字符串設置backgroundColor。使用backgroundcolor將標籤字符串設置爲標籤

NSString *string = @"This is a UILAbel with two lines"; 

NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString: 
              string]; 

[mutableString setAttributes:@{NSBackgroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(0, string.length)]; 

如果文本產生換行符,則將backgroundColor填充到右邊框,在第二行中它將停在最後一個字符處。

但我想第一行中的backgroundcolor也設置爲最後一個字符。

Pic example

+0

你可以顯示你的問題或你想要的圖像。 –

+0

你是如何得到正確的照片? –

+0

您正在將背景顏色設置爲紅色...對我來說看起來很漂亮:p –

回答

0

backgroundColor屬性應用於代表所述標籤的整個矩形。

要解決此問題,您需要根據其內容動態調整UILabel的框架,以達到預期的效果。

UILabel上有一個叫做sizeToFit的方法;嘗試調用它。

儘管我建議您使用autolayout,並讓標籤根據約束來調整自身大小。

相關問題