Reputation: 973
I'm trying to fill a UITableView with HTML strings. Getting the strings, putting them into cell etc. everything is ok. And I want to change my NSAttributedText's font and font size. I wrote the code below.
I checked the code via the first NSLog, if my UIFont is in the dictionary as expected; so it is in the dictionary. still no problem. but my font and font size is not changing on runtime. There is no console error. Just not working.
Second NSLog is for checking the attributedString's font and size. It says, my string is "Times New Roman" with fontSize:12. But as you will see in the code, I'm trying to make it "Verdana" with Size 15
Can anyone help me about this? Thanks.
PS: I'm keeping the strings in some objects. obj.name is the string which I want to write into table cell.
Html strings have some unwanted tags, so the function [self clearTheHtml:obj.name] function is clearing them.
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
UIFont *font = [UIFont fontWithName:@"Verdana" size:15];
[attributes setObject:font forKey:NSFontAttributeName];
NSString *s = [self clearTheHtml:obj.name];
NSLog(@"%@", [attributes objectForKey:NSFontAttributeName]); //to see if my font is not in the dictionary.
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[s dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:&attributes error:nil];
NSLog(@"%@", attributedString);
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
cell.textLabel.attributedText = attributedString;
EDIT:
here is the NSString *s :
2014-01-25 07:38:04.527 KadirB[7483:70b]
''Narakas 2013 Brachial Plexus Surgery Symposıum'' 23-25 Mayıs 2013 tarihleri arasında Montreux, İsviçre de yapıldı. Brakial Plexus sorunlarına yönelik son gelişmeler toplantıda tartışıldı.
Upvotes: 3
Views: 8474
Reputation: 3598
I've had the same problem just a couple of days ago. I solved by simply changing the implementation like so:
...
NSMutableAttributedString *attributedString = ...;
[attributedString addAttributes:@{NSFontAttributeName: font} range:NSMakeRange(0, attributedString.length)];
I hope it helps you as well.
Upvotes: 6