简体   繁体   English

更改 html 中图像的宽度和高度 swift IOS 中的文本

[英]Change width and height of image in html text in swift IOS

I have html text returned from the server and I am using a UITextView on the IOS client to render the text.我有从服务器返回的 html 文本,我在 IOS 客户端上使用 UITextView 来呈现文本。 I am first converting the html string to attributed string and then enumerating over the attributes and changing the font, foreground color of the text as needed.我首先将 html 字符串转换为属性字符串,然后枚举属性并根据需要更改文本的字体、前景色。 Finally I am setting the attributed text of UiTextView.最后我设置了 UiTextView 的属性文本。 However, when the html contains images and I see that they get cut off when rendered in UITextView.但是,当 html 包含图像时,我看到它们在 UITextView 中呈现时被截断。

Any pointers on how I could change the width and height of images contained in the html text in order to fit my phone screen width?关于如何更改 html 文本中包含的图像的宽度和高度以适应我的手机屏幕宽度的任何指示?

I use SwiftSoup to extract data from html or edit the HTML string.我使用SwiftSoup从 html 中提取数据或编辑 HTML 字符串。 I also have found that when images are present it's easier to manage the layout and format inside a WKWebView .我还发现,当图像出现时,在WKWebView中管理布局和格式会更容易。

This example also helps to add support for dark mode in your WKWebView.此示例还有助于在 WKWebView 中添加对暗模式的支持。

var newsRssBody: String = ""
let css = "@media (prefers-color-scheme: dark) {body { background-color: rgb(38,38,41); color: white; } a:link { color: #0096e2; } a:visited { color: #9d57df; } }"
try doc.head()?.append("<style>\(css)</style>")
self.newsRssBody = "\(doc)"

The headerString line helps with content sizing based on the device. headerString 行有助于根据设备调整内容大小。

let headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header>"
self.webView.loadHTMLString(headerSting + self.newsRssBody, baseURL: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM