UILabel Character spacing to include background and underline attributes - ios

I am wanting a UILabel where each character is underlined and has a background, while also having a space between the background of each individual character - so essentially each character is in it's own "box"
I have tried using attributedText on a UILabel but having set the NSBackgroundColorAttributeName and NSKernAttributeName the background fills the entire label, essentially I'd like a gap of clear color between each letter.
Is there a way of doing this with attributed text, or will this need to be resolved with another solution?
I currently have tried the following as an example:
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:#"EXAMPLE"];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)]
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)]
[string addAttribute:NSKernAttributeName value:#(5) range:NSMakeRange(0, [string length])];
[label setAttributedText:string];
Current result:
Desired Result:
(you can ignore the colors, this is just an example)

Related

UITextview font styling issue

I am setting font to UITextView . It works. I am also making the text entered in the UITextView as bold by selecting the text in UITextView. My problem is if I give
font size I cannot make the text bold.
try it .
self.txtView.text=#“Life is an opportunity , Don’t west your time !”;
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.txtView.text];
NSString *word= #"Life is an opportunity";
NSRange range=[self..txtView.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
[self.txtView setAttributedText:string];
[self.txtView setFont:[UIFont fontWithName:#"Montserrat-Bold" size:15]];
self.txtView.alpha=1;

Break UILabel into 2 lines with separate background?

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];

How to change UILable text color change for a certain words? [duplicate]

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(, )];

Can i set both current time and buffer limit on the same indicator

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

Different colors in UITextView

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

Resources