NSMutableAttributedString hides part of a NSString - iOS - ios

I have a NSString with this format:
<p>When: Every day except Monday at 11:45am
</p><p>Where: Main Pool</p><p><br></p><p>Get refreshed in the main pool during the warm sunny days whilst participating in our animation's fit activities. Enjoy our aqua fitness 6 times per week.<br></p>
I use this code to display the html attributes and also set a default font size and color:
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[self.Description dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
__block BOOL found = NO;
[attrStr beginEditing];
[attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:16];
[attrStr addAttribute:NSFontAttributeName value:newFont range:range];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor darkGrayColor]
range:range];
found = YES;
}
}];
[attrStr endEditing];
cell.descLbl.attributedText = attrStr;
Those words doesn't show up:
fitness 6 times per week.<br></p>
it only shows some weird dots-lines at the top of where the letters should be.
I believe the problem is when I change the String to unicode, the length of the String changes and so the range. How can I fix this?

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 change the font of attributed string in iOS? [duplicate]

This question already has an answer here:
Apply Custom font to Attributed string Which Converts from HTML String
(1 answer)
Closed 6 years ago.
I have added attributed string on label.I am displaying html text which works fine but by default it is always showing Times new roman family. Please tell how can i change text family.
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
Try with this:
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSMutableAttributedString *newString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
NSRange range = (NSRange){0,[newString length]};
[newString enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
UIFont *replacementFont = [UIFont fontWithName:#"Palatino-Roman" size:14.0];
[newString addAttribute:NSFontAttributeName value:replacementFont range:range];
}];
self.label.attributedText = newString;
you can do like that:
NSDictionary *attrDict = #{
NSFontAttributeName : [UIFont fontWithName:Arial size:16.0],
NSForegroundColorAttributeName : [UIColor redColor]
};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"string" attributes:attrDict];

UIText View attributed string does not change

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

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.

How to show multiple line text that has different font without using multiple Labels

I have to show a text paragraph that contains few words in bold font. ex.
If I use different labels then on changing the orientation it does not resize properly.
Can anyone tell me what can the best way to do it.
You can use an UITextView using NSAttributedString (have a look to the apple doc)
And you have an explanation of how to use it here.
You can find the range of your word and change the font or the color or whatever using :
- (IBAction)colorWord:(id)sender {
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:self.text.text];
NSArray *words = [self.text.text componentsSeparatedByString:#" "];
for (NSString *word in words)
{
if ([word hasPrefix:#"#"])
{
NSRange range=[self.text.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
}
}
[self.text setAttributedText:string];
}
You have to use a NSAttributedString and assign it to the UITextField, this is an example:
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:yourString];
[myAttributedString addAttribute:NSFontAttributeName
value:boldFont
range:NSMakeRange(0, 2)];
[myAttributedString addAttribute:NSFontAttributeName
value:regularFont
range:NSMakeRange(3, 5)];
[self.description setAttributedText:myAttributedString];
Find all the doc here:
NSAttributedString
For your case, you can use a webView and load it with your string:
[webView loadHTMLString:[NSString stringWithFormat:#"<html><body style=\"background-color: transparent;\">This is <b><i>Test</b></i> dummy text ..</body></html>"] baseURL:nil];

Resources