如何仅将背景颜色设置为UILabel文本 [英] How to set background color to UILabel text only
本文介绍了如何仅将背景颜色设置为UILabel文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用需要设置标签(如附件图像)的应用程序.请让我知道是否有人有想法吗?
I'm working the App where i need to set the label like attached image. Please let me know if anyone have any idea?
推荐答案
您可以这样做
NSString *yourString1=@"What Does your friends really";
NSString *yourString2=@"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;
此代码的输出:
这篇关于如何仅将背景颜色设置为UILabel文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文