一般來說iOS端顯示html標簽用的都是UIWebView或WKWebView,做的比較方便快捷,但是加載速度也是比較慢的,所以這里記錄並推薦一種比較簡單的方式:
///顯示富文本 +(NSAttributedString *)showAttributedToHtml:(NSString *)html withWidth:(float)width{ //替換圖片的高度為屏幕的高度 NSString *newString = [html stringByReplacingOccurrencesOfString:@"<img" withString:[NSString stringWithFormat:@"<img width=\"%f\"",width]]; NSAttributedString *attributeString=[[NSAttributedString alloc] initWithData:[newString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; return attributeString; }
加載html標簽的速度有所提升,不過卻在此發現一個問題,無論是webView還是富文本都不支持jpeg圖片的格式,有知道的大神們,幫忙留下一個答案,十分感謝!!