iOS 7 using an HTML string as an NSAttributedString AND setting the font?

亡梦爱人 提交于 2019-12-11 03:28:11

问题


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?


回答1:


The way I was able to get it to work was to prepend my string with <span style=\"font-family: Avenir; font-size: 12\">%@</span>




回答2:


For an objective-c approach, I used NSMutableString and -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)];


来源:https://stackoverflow.com/questions/21318276/ios-7-using-an-html-string-as-an-nsattributedstring-and-setting-the-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!