2

I have a UITextView in which I'm trying to display some attributed text which comes in the form of HTML. It uses things like inline bolding and italicizing. The client now wants the font on this to change. However, whenever I change the font, it seems to disregard any of the HTML formatting. Is there any way to change the font while retaining the other formatting attributes, like bolding an italicizing?

4

2 回答 2

5

我能够让它工作的方法是在我的字符串前面加上<span style=\"font-family: Avenir; font-size: 12\">%@</span>

于 2014-01-24T22:01:34.693 回答
1

对于 Objective-C 方法,我使用了NSMutableString-setAttributes:

NSString *htmlString = @"..."
NSData *textData = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
      initWithData:textData
           options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType }
documentAttributes:nil error:nil];
[attrStr setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"avenir" size:12.0f]} range:NSMakeRange(0,attrStr.length)];
于 2015-08-04T07:03:11.450 回答