How to set background color to UILabel text only

不想你离开。 提交于 2019-12-20 07:40:07

问题


I'm working the App where i need to set the label like attached image. Please let me know if anyone have any idea?


回答1:


You can do like this

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;

Output of this Code:




回答2:


If using an attributed string with a background color on the string doesn't work then you might need to create 2 separate labels with space between them and set the background color on each.




回答3:


May be you need some space with that background color where text has ended. I have solved this problem in my own tricks. Add some comma or point to separate the string. Then apply this to the string. No extra label need to create.

  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;

My output is like:

Now make the K's background (title2) clear, so you can get the spaces after text end with your space!




回答4:


//setting dummy text to label
self.lbLog.text=@"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;



回答5:


lblText.backgroundColor=UIColor.red



回答6:


You need to use separate label where you set background colour of the label if background colour of all label text is same for all text in label otherwise for different background colour for some of the text you need to use NSMutableAttributedString.

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



回答7:


Use two custom Label (or instead label use uiview as your background), One is for your background,set backgroundcolor as clear color and another label is for ur text set backgroundcolor as your desire color.




回答8:


We can do this with the help of UItextview. I did that, I will post the code very soon.



来源:https://stackoverflow.com/questions/38249729/how-to-set-background-color-to-uilabel-text-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!