2016-12-07 101 views
0

我需要創造這樣的事情。的iOS設置中的UILabel文本背景色(用圓角矩形)

enter image description here

其中一個方法是創建這樣的。我可以附加圖片(對於「PDF」)。

NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"MyIcon.png"]; 

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; 

NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"]; [myString appendAttributedString:attachmentString]; 

myLabel.attributedText = myString; 

但我不確定它是否好。有其他方法嗎?我可以突出顯示具有圓形背景的「PDF」字嗎?我可以使用屬性字符串輕鬆設置文本背景顏色,但不會有圓角。

+0

你爲什麼不嘗試使用標籤的層??篩選label.layer.cornerRadius = 7的cornerRadius財產; –

+0

您能否詳細闡述一下?我只有1個標籤,只需要更改'PDF'的背景顏色。 –

+0

PDF是你的標籤吧? –

回答

1

我將嵌入的UILabel的PDF與所需的背景顏色另一個的UIView,並繞行view.layer.cornerRadius角落。這樣,您可以在圓形視圖中調整UILabel的方式。

如果你圓上的UILabel角落,你可能有你的文字重疊的圓邊。

4
myLabel.backgroundColor = [UIColor redColor]; 
myLabel.layer.cornerRedius = 5.0; 
myLabel.laayer.maskToBounds = YES; 
1

使用RTLabel庫轉換HTML文本。我已經使用過好幾次了。有用。這裏是鏈接到庫和示例代碼。

https://github.com/honcheng/RTLabel

然後應用你需要的css樣式

希望我幫了忙。

+0

謝謝。我會試一試。 –