我在工作的应用程序,我需要设置标签像附加图像。请让我知道,如果有人有任何想法? 如何将背景颜色设置为仅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. 如何为NSTableCellView设置背景颜色?
- 4. 如何为BottomBar设置背景颜色?
- 5. $。将背景颜色设置为当前设置的颜色
- 6. 将背景颜色设置为progressBar
- 7. 将背景颜色设置为DotNet.Highcharts
- 8. 将背景设置为渐变颜色
- 9. 将背景颜色设置为页面
- 10. 将背景颜色设置为纹理
- 11. 如何将渐变色设置为iPhone中的UILabel背景
- 12. 如何以编程方式设置UILabel的背景颜色?
- 13. 将背景颜色设置为色调颜色
- 14. 如何将背景颜色设置为白色
- 15. 如何将IFRAME的背景颜色设置为其父文档的BG颜色?
- 16. 如何动画UILabel的背景颜色?
- 17. Qt:如何设置QPushButton的背景颜色为系统颜色?
- 18. 仅将背景颜色强制为纯文本
- 19. 根据背景颜色设置文本的颜色
- 20. 将div的背景颜色的80%设置为一种颜色
- 21. 将Windows窗体背景颜色设置为混合颜色?
- 22. 将gridview背景颜色设置为datatable的颜色?
- 23. 如何将viewcontroller的背景颜色从灰色更改为设置的颜色?
- 24. 设置背景颜色2
- 25. 设置背景颜色
- 26. Angular4背景颜色设置
- 27. 设置背景颜色[JAVA]
- 28. angular2设置背景颜色
- 29. 设置背景颜色
- 30. 设置背景颜色
也许用'NSAttributedString'和'NSBackgroundAttributeName'? – Larme