http://imgur.com/yVkhHon.jpg
I want my text on lines but with separate background. How can i achieve this using UILabel ?
I tried padding etc but nothing works it just show single red box if i select background color for label.
Any help on how to get same effect like image ?
Use the following code:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.yourLabel.text];
//EDITED
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text.length/2)];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(text.length/2, text.length/2)];
[self.yourLabel setAttributedText: text];
Use NSAttributesString. Create a UILabel, create an NSAttributesString. Then instead of using setText of label, call setAttributedText using the string object you created earlier.
More help on Apple Documentation
[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange];
Related
I am new in iOS. I want to know how can I change the color for each part of UIButton title.
For example, a part of title in my UIButton is black and another the part of title in my UIButton is yellow
This is the describe image
Any help would be appreciated
U can use NSMutableAttributedString then add it to button with [button setAttributedTitle:attString forState:UIControlStateNormal];
To change color of a range in the Attribute String, read this, basically its just 1 of the attribute dictionary
Read this SO Question
I solve my question by using the code below.
For UILabel
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.lblFriendBand.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(1, 2)];
[self.lblFriendBand setAttributedText: text];
For UIButton
NSMutableAttributedString *text2 =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.btnFriendName.titleLabel.attributedText];
[text2 addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(1, 2)];
[self.btnFriendName setAttributedTitle:text2 forState:UIControlStateNormal];
This question already has answers here:
UILabel with text of two different colors
(20 answers)
Closed 8 years ago.
Say i have UIlabel and i want to change color of text at certain word. For example my label text is
"Shop now and get up 50% off Select shoes Limited time offer"
i which "50%" and "Limited" want change color
predefined some word if accrue in label text then change color. fist find and then change color and font or sub string.
To do this use NSAttributedString:
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: label.attributedText];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(10, 1)];
[label setAttributedText: text];
For details :https://github.com/joaoffcosta/UILabel-FormattedText
You can use NSMutableAttributedString
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Shop now and get up 50% off Select shoes Limited time offer"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(, )];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(, )];
I want to add mandatory field mark in textfield placeholder text like below image please suggest me i am totally confused how to do it
You can use :
NSMutableAttributedString *attriButedString = [[NSMutableAttributedString alloc]initWithString:#"Mandatory*"];
[attriButedString addAttribute:NSForegroundColorAttributeName
value:[NSColor lightGrayColor]
range:NSMakeRange(0, 9)];
[attriButedString addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(9, 1)];
[[self.textField cell] setPlaceholderAttributedString:attriButedString];
This will look like as:
Note: Instead of hard-coded range you can calculate according to your stringlength.
If you are creating TextField from xib click on text field and go to attribute inspecter and fill your placeholder name in placeholder text feild
Try with this.....
[tfSubject setValue:[UIColor colorWithRed:249.0/255.0 green:204.0/255.0 blue:88.0/255.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
I need to show both current time (blue color) and buffer limit (gray color) on the same UIProgressView for my AVPlayer instance. Is it possible?
You need to use NSAttributedStrings to accomplish this.
Attributed Strings
Here is an example...
Make a custom UIView that contains a UILabel and UIProgressView as subviews.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Hello. That is a test attributed string."];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
label.attributedText = str;
And here is an example of a custom UIProgressView implementation
I try to add string with different colors in my UITextView. I wrote this code
NSMutableAttributedString* attString =
[[NSMutableAttributedString alloc]initWithString:view.text]; //view is my UITextView
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:[UIColor greenColor]
range:(NSRange){attString.length-8, 8}];
view.attributedText = attString;
Maybe it is incorrect attribute, can you tell me what attribute change color for text?
Try using NSForegroundColorAttributeName instead of kCTForegroundColorAttributeName.
NSAttributedString attribute keys