如何仅将背景颜色设置为 UILabel 文本

use*_*145 -1 objective-c uilabel ipad ios

我正在使用应用程序,我需要设置标签(如附加图像)。如果有人有任何想法,请告诉我?

图片演示

Har*_*hah 5

你可以这样做

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;
Run Code Online (Sandbox Code Playgroud)

该代码的输出:

在此输入图像描述