iOS - 调整 HTML 字符串中的图像大小

iOS - Resizing images in HTML string

我有一个 HTML 的字符串,我正在像这样将其转换为 NSAttributedString。

NSAttributedString *decodedString = [[NSAttributedString alloc] initWithData:[encodedString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

HTML 中可能包含图像 <img... - 字符串根据您从中提取 HTML 的网站而变化。

图像显示在我的 TextView 中,但非常大。

如何调整所有图像的大小(按百分比调整,以便它们保持正确的形状)以适合我的 TextView?

只是给你 core/basic 想法来处理它。

--> 使用 NSRange 并在 HTML 字符串中搜索,找到所有 <img.. 标签并制作 Substrings.

--> 现在在 <img... 标签的这些子字符串中获取 widthheight 并根据您的需要调整图像 widthheight 的大小所需的大小并在 HTML.

中的 <img.. 标签内替换这些大小(替换字符串值)

现在从这个更新后的 HTML 字符串中生成 NSAttributedString

就是这样.... :)

这一定会对您有所帮助。