3

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ı.
2
  • Can you produce an example of the HTML that exists in "s"? Commented Jan 24, 2014 at 22:35
  • @KendallHelmstetterGelner I edited and added the NSString *s.. Check pls. My htmlCleaner erases the <style> and <span> tags. There is no font or font-size coming with html. Commented Jan 25, 2014 at 5:42

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, it's still same. No luck.
SORYY SORRYYY!!! My fault.. When I tried, I was awake for 5 minutes.. I have done a mistake and didn't worked. But now it's ok. Thank you very much.
Not a perfect solution, as it overrides bold tags. Here's a solution that maintains bold text - stackoverflow.com/questions/19921972/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.