I'm working the App where i need to set the label like attached image. Please let me know if anyone have any idea?
7 Answers
Answers 1
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.
Answers 2
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!
Answers 3
//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;
Answers 4
lblText.backgroundColor=UIColor.red
Answers 5
You can do like this
NSString *str1=@"What Does your friends really\n"; NSString *str2=@"Think of your spouce?"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]]; [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:
Answers 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];
Answers 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.
0 comments:
Post a Comment