UIText View attributed string does not change - ios

I am trying set the attribute string font to 18. Here is code
NSString *str = [NSString stringWithFormat: #"We notice you've been pre-registrated here. Can you please confirm you are from %#?", visitor.company_name];
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:str];
NSRange range = [str.lowercaseString rangeOfString:visitor.company_name.lowercaseString];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, str.length)];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:18.0] range:range];
self.textView.attributedText = string;
This only increases the size of company name. The string size does not change from the original size.

This line saved my life:
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica" size:18.0] range:NSMakeRange(0, str.length)];

The problem is the line defining the string range.
NSRange range = NSMakeRange(0,string.length);
[string addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0,string.length)];

Related

iOS: how to change colour/font of some characters of a string

in my app i have to change the font of a part of string which comes from JSON response
> "<span class=\"drop_data_user\">Andrew James</span> liked your comment
> \"hiiiiiiiiiiiiiii\" that you posted."
to convert it in attributed string i am using the following code
NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[NotificationTxt dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:#(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
I want to change the font colour of the sender
Also, you can use it as follows.
NSString *dateString = [NSString stringWithFormat:#"%#%#", #"Teslimat: ", selectedReservationModel.DateLabel];
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString: dateString];
[attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansBoldFontSize:12] range:NSMakeRange(0, 8)];
[attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansRegularFontSize:12] range:NSMakeRange(9, selectedReservationModel.DateLabel.length)];
lblDeliveryDate.attributedText = attrS;
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"My String"];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 6)];
self.myLabel.attributedText = attrS;
For fonts use
[attrS addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 3)];
I believe the easiest way to fulfil your task is to use CSS inlined in the text, then parsing HTML as you do will change the color too (I'm using Swift syntax, but I believe you can easily convert it to ObjC):
let text = "<span class=\"drop_data_user\">Andrew James</span> liked your comment \"hiiiiiiiiiiiiiii\" that you posted."
let colouredAuthorText = "\(text)<style>.drop_data_user { color: #FEB600; }</style>"
Then just parse colouredAuthorText (which is created by appending style to the original text) instead of the original, and you should be good to go.

how to use round corner text in uilabel in iphone

I have a label Like,
I want to display this single label with different font colors.
i.e..,
"First" string should be in redcolor.
"&" text should be roundcornerd and lightgraycolor.
Use nsattributedstring
https://developer.apple.com/reference/foundation/nsattributedstring
Example use
How do you use NSAttributedString?
try this
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:singlestoreNode.objSeName];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,2)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(2,3)];
yourlable.attributedText = string;
yourlable.layer.cornerRadius = 8.0;
You can achieve that by creating 3 labels.
Set the colors for the label like this:
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:lblTemp.text];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
lblTemp.attributedText = string;
For the round background in "&" you can do the following:
[yourUILabel sizeToFit];
yourUILabel.backgroundColor = [UIColor lightGrayColor];
yourUILabel.layer.masksToBounds = YES;
yourUILabel.layer.cornerRadius = yourUILabel.frame.size.width/2;
Then you just need to align everything.

How to set background color to UILabel text only

I'm working the App where i need to set the label like attached image. Please let me know if anyone have any idea?
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:
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.
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!
//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;
lblText.backgroundColor=UIColor.red
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];
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.
We can do this with the help of UItextview. I did that, I will post the code very soon.

How can I scan a string for numbers to adjust the font size in an iOS app?

I'm off to a good start. But I would like to make this code more dynamic.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *font = [UIFont systemFontOfSize:10.0f];
UIFont *smallFont = [UIFont systemFontOfSize:9.0f];
[attString beginEditing];
[attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];
Say I have the following String:
#"C3OC2OH4"
I would like to adjust the font size of the numbers only to fulfill a chemistry application. How can I scan the above string, and create a series of custom ranges to plug into my function above, which adjusts the font size?
You can enumerate the characters and apply the attributes like this.
NSString * string = #"C3OC2OH4";
NSMutableAttributedString * attributedString = [NSMutableAttributedString new];
NSDictionary * subscriptAttributes = #{NSFontAttributeName : [UIFont systemFontOfSize:10.0],
NSBaselineOffsetAttributeName : #(-5.0)};
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSCharacterSet * letterCharacterSet = [NSCharacterSet letterCharacterSet];
if ([substring rangeOfCharacterFromSet:letterCharacterSet].location != NSNotFound)
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring]];
else
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:substring attributes:subscriptAttributes]];
}];
In this case substring is each character and is appended without any change if it is a letter. If it is a number we change the font size and shift the baseline. Change them according to your need.
Good luck.

Adding color to text in TextView

I have a TextView in which I need some text to change colors and fonts. Any method is available other than core text. Please help.
i need some text to change colors and fonts.
You certainly looking for NSAttributedString
From Source
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;

Resources