iOS UITextView 富文本顯示html標簽


   一般來說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圖片的格式,有知道的大神們,幫忙留下一個答案,十分感謝!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM