ios7 font size change when create nsattributedstring from html

后端 未结 5 572
醉梦人生
醉梦人生 2020-11-29 06:57

I have a UITextView where I\'m managing an NSAttributedString, initially entered as normal via the keyboard. I save the attributed string as HTML, which looks fine. When I

5条回答
  •  萌比男神i
    2020-11-29 07:21

    Using and css works for me, following Parsing HTML into NSAttributedText - how to set font?:

    // HTML -> NSAttributedString
    + (NSAttributedString *)attributedStringFromHTML:(NSString *)html {
        NSError *error;
        NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:&error];
        if(!attrString) {
            NSLog(@"creating attributed string from HTML failed: %@", error.debugDescription);
        }
        return attrString;
    }
    
    // force font thrugh  & css
    + (NSAttributedString *)attributedStringFromHTML:(NSString *)html withFont:(UIFont *)font    {
        return [Util attributedStringFromHTML:[NSString stringWithFormat:@"%@", font.fontName, font.pointSize, html]];
    }
    

    This sets the font name and size but doesn't impact the styles.

提交回复
热议问题