# UITextView-html-demo **Repository Path**: ihongren/UITextView-html-demo ## Basic Information - **Project Name**: UITextView-html-demo - **Description**: UITextView 加载 HTML 文本字符串的一些优化和注意事项 Demo - **Primary Language**: Objective-C - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-31 - **Last Updated**: 2025-11-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # iOS UITextView 加载 HTML 时的问题与优化 #### 在 iOS 中如果想加载显示 HTML 文本,一般有以下的几种方案: 1. 使用 WKWebView ,偏重、性能较差 2. 将 HTML 字符串转换为 `NSAttributedString` 对象,使用 UITextView,UILabel... 3. 使用一些三方库,如 DTCoreText、SwiftSoup 4. 自己去解析标签实现,较复杂。 对于一些详情原生页面,加载一段功能简单的 html 标签文本,使用 `NSAttributedString + UITextView` 是一种相对轻量的选择,本文也只讨论这种方式。 ### 然而在实际开发的过程中,我们很容易发现一些问题: ##### 1、html 字符串转 NSAttributedString 是同步的,文本稍大一点,就会阻塞主线程,页面卡死。 这个问题好解决,直接将转换操作放到子线程去做就好 ```objective-c dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSAttributedString *att = [htmlString htmlToAttr]; dispatch_async(dispatch_get_main_queue(), ^{ self.textView.attributedText = att; }); }); ``` ##### 2、如果只是一些片段 html 标签,转换后的样式可能不太美观,可以加一些 CSS 来美化。 比如字体默认太小,图片显示太宽等。这时我们可以自己拼接一些 CSS 进去,下面代码我们增加默认字体大小 16px,图片宽度为 textView 宽度,高度自适应。 ```objective-c CGFloat contentWidth = self.textView.bounds.size.width; NSString *newHtml = [NSString stringWithFormat:@"
%@",@"{font-size:16px;}",contentWidth,html]; ``` 你也可以去遍历 `NSParagraphStyleAttributeName` 属性,来设置一些 style。 ```objective-c // 设置行高 - (NSAttributedString*)addLineHeight:(CGFloat)lineHeight attr:(NSAttributedString*)attr { [attr enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attr.length) options:(NSAttributedStringEnumerationLongestEffectiveRangeNotRequired) usingBlock:^(NSMutableParagraphStyle *style, NSRange range, BOOL * _Nonnull stop) { NSAttributedString *att = [attr attributedSubstringFromRange:range]; // 忽略 table 标签 if (![[att description] containsString:@"NSTextTableBlock"]) { style.lineSpacing = lineHeight; } }]; return attr; } ``` ##### 3、加载图片过大、多图时,首次显示很慢,拼网络了。 这种时候,我们可以先使用正则找出所有的 `