2016-07-07 34 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的帮助下做到这一点。我这样做了,我很快就会发布代码。