我在工作的應用程序,我需要設置標籤像附加圖像。請讓我知道,如果有人有任何想法? 如何將背景顏色設置爲僅UILabel文本
回答
//setting dummy text to label
[email protected]"This is Simple Text With Red background Color";
//creating attributed string
NSMutableAttributedString *attribString =
[[NSMutableAttributedString alloc] initWithString:self.lbLog.text];
//setting background color to attributed text
[attribString addAttribute:NSBackgroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, attribString.length)];
//setting attributed text to label
self.lbLog.attributedText = attribString;
@ Mahesh感謝您的回覆,但它只是改變背景顏色。實際上,我想將顏色設置爲帶有間隙透明的單個標籤的每一行 – user3306145
@ user3306145然後,您需要使用'NSParagraphAttributeName'和'NS(可變)ParagraphStyle',您可以在其中設置行高(如果需要)。 – Larme
好的,但我需要像兩個標籤不同的框架。如果我現在就按照你的建議實施它,但是我想要空間與清晰的顏色。 – user3306145
如果使用屬性串與串背景顏色不工作,那麼你可能需要創建與他們之間的空間,2個獨立的標籤,並設置在每個背景顏色。
可能是你需要一些空間與文本已經結束的背景顏色。我用自己的技巧解決了這個問題。添加一些逗號或點來分隔字符串。然後將其應用於字符串。沒有額外的標籤需要創建。
NSArray *aArray = [@" Font Size .k" componentsSeparatedByString:@".k"];
NSMutableAttributedString *fulltext=[[NSMutableAttributedString alloc] initWithString:@""];
NSMutableAttributedString *title1=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:0]];
NSMutableAttributedString *title2=[[NSMutableAttributedString alloc] initWithString:[aArray objectAtIndex:1]];
//[title1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0] range:NSMakeRange(0,title1.length)];
[title1 addAttribute:NSBackgroundColorAttributeName
value:[UIColor colorWithRed:255.0/255.0 green:84.0/255.0 blue:49.0/255.0 alpha:1.0]
range:NSMakeRange(0, title1.length)];
[title1 addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f]
range:NSMakeRange(0,title1.length)];
[title2 addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,title2.length)];
[title2 addAttribute:NSBackgroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(0, title2.length)];
[title2 addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:(isIpad||isIPadPro)?19.0f:16.0f]
range:NSMakeRange(0,title2.length)];
[fulltext appendAttributedString:title1];
[fulltext appendAttributedString:title2];
self.textLabel.attributedText = fulltext;
我的輸出是這樣的:
現在讓K公司的背景(標題2)明確的,所以你可以將文本後您的空間,最終獲取的空間!
lblText.backgroundColor=UIColor.red
你可以這樣做
NSString *[email protected]"What Does your friends really";
NSString *[email protected]"Think of your spouce?";
NSString *str1=[NSString stringWithFormat:@" %@..\n",yourString1];
NSString *str2=[NSString stringWithFormat:@" %@,,",yourString2];
NSString *str3=[NSString stringWithFormat:@"%@%@",str1,str2];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]];
NSRange range = [str1 rangeOfString:@".."];
NSRange range1 = [str3 rangeOfString:@",,"];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:range1];
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor orangeColor] range: NSMakeRange(0, str1.length)];
[attributedString addAttribute: NSBackgroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(str1.length, str2.length)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:5];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str2 length])];
lblTest.attributedText = attributedString;
本規則的輸出:
你的NSString輸出是什麼* str1 = @「你的朋友真的是什麼」; //最後有一些空間? End Space背景顏色必須被覆蓋。但是你的解決方案沒有。 –
@JamshedAlam - 請檢查最新的答案。 –
現在,這可能是解決方案。感謝您的幫助:) –
您需要使用您設置標籤的背景顏色單獨的標籤,如果背景顏色的所有標籤文字都是相同的標籤中的所有文字,否則對於不同的背景顏色對於您的某些文字需要使用NSMutableAttributedString。
label.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0];
它會改變背景顏色。我們需要更改唯一的文本顏色。 – user3306145
label.textColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0]; – Miti
使用兩個自定義標籤(或標籤,而不是使用的UIView作爲背景),一個是你的背景設置backgroundColor清晰的色彩和另一個標籤是烏拉圭回合文本需設置backgroundColor爲你的願望顏色。
我們可以在UItextview的幫助下做到這一點。我這樣做了,我很快就會發布代碼。
- 1. UILabel設置透明背景顏色?
- 2. wpf textblock - 僅在文本背後設置背景顏色?
- 3. 如何爲GridViewColumn設置背景顏色
- 4. 將背景顏色設置爲DotNet.Highcharts
- 5. 將背景顏色設置爲頁面
- 6. 如何將漸變色設置爲iPhone中的UILabel背景
- 7. 如何以編程方式設置UILabel的背景顏色?
- 8. 如何將背景顏色設置爲白色
- 9. 如何動畫UILabel的背景顏色?
- 10. 如何將IFRAME的背景顏色設置爲其父文檔的BG顏色?
- 11. Qt:如何設置QPushButton的背景顏色爲系統顏色?
- 12. 將gridview背景顏色設置爲datatable的顏色?
- 13. Angular4背景顏色設置
- 14. 設置VIM背景顏色
- 15. 設置背景顏色toolbaritems
- 16. 設置背景顏色CMDIFrameWnd
- 17. 設置行背景顏色
- 18. 設置背景顏色
- 19. TreeCellRenderer:如何設置背景顏色?
- 20. 如何設置背景顏色?
- 21. 如何設置tableView的背景顏色
- 22. 如何設置AlertDialog背景顏色
- 23. 如何設置JQGRID行背景顏色?
- 24. 如何設置UIWebView的背景顏色
- 25. 如何設置RelativeLayout背景顏色?
- 26. 如何設置標籤背景顏色
- 27. 如何設置UIWebView的背景顏色?
- 28. 如何設置Canvas3D背景顏色?
- 29. 如何設置Qtextedit背景顏色?
- 30. 輸入文字背景顏色設置爲黃色,僅在鉻色
也許用'NSAttributedString'和'NSBackgroundAttributeName'? – Larme