2016-07-07 91 views

回答

2
//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; 
+0

@ Mahesh感謝您的回覆,但它只是改變背景顏色。實際上,我想將顏色設置爲帶有間隙透明的單個標籤的每一行 – user3306145

+0

@ user3306145然後,您需要使用'NSParagraphAttributeName'和'NS(可變)ParagraphStyle',您可以在其中設置行高(如果需要)。 – Larme

+0

好的,但我需要像兩個標籤不同的框架。如果我現在就按照你的建議實施它,但是我想要空間與清晰的顏色。 – user3306145

3

如果使用屬性串與串背景顏色不工作,那麼你可能需要創建與他們之間的空間,2個獨立的標籤,並設置在每個背景顏色。

3

可能是你需要一些空間與文本已經結束的背景顏色。我用自己的技巧解決了這個問題。添加一些逗號或點來分隔字符串。然後將其應用於字符串。沒有額外的標籤需要創建。

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; 

我的輸出是這樣的:

enter image description here

現在讓K公司的背景(標題2)明確的,所以你可以將文本後您的空間,最終獲取的空間!

2
lblText.backgroundColor=UIColor.red 
4

你可以這樣做

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; 

本規則的輸出:

enter image description here

+0

你的NSString輸出是什麼* str1 = @「你的朋友真的是什麼」; //最後有一些空間? End Space背景顏色必須被覆蓋。但是你的解決方案沒有。 –

+1

@JamshedAlam - 請檢查最新的答案。 –

+0

現在,這可能是解決方案。感謝您的幫助:) –

0

您需要使用您設置標籤的背景顏色單獨的標籤,如果背景顏色的所有標籤文字都是相同的標籤中的所有文字,否則對於不同的背景顏色對於您的某些文字需要使用NSMutableAttributedString

label.backgroundColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0]; 
+0

它會改變背景顏色。我們需要更改唯一的文本顏色。 – user3306145

+0

label.textColor = [UIColor colorWithRed:29.0/255.0 green:135.0/255.0 blue:145.0/255.0 alpha:1.0]; – Miti

0

使用兩個自定義標籤(或標籤,而不是使用的UIView作爲背景),一個是你的背景設置backgroundColor清晰的色彩和另一個標籤是烏拉圭回合文本需設置backgroundColor爲你的願望顏色。

0

我們可以在UItextview的幫助下做到這一點。我這樣做了,我很快就會發布代碼。