iOS UITextView HTML: 居中图片
iOS UITextView HTML: center image
我正在尝试在我的 iOS 应用程序的 UITextView 中加载 html 内容。
我使用以下 html 代码:
<meta charset=utf-8>
<h3 style="text-align: center;">Sample text.</h3>
<p><img style="display: block; margin-left: auto; margin-right: auto;" src="https://cdn.sstatic.net/Sites/Whosebug/company/img/logos/so/so-logo.png?v=9c558ec15d8a" width="182" height="44"/></p>
所以它应该在中心显示文本和图像,但是图像没有居中:
有谁知道如何将图像居中对齐?
以下是我设置 html 内容的方法:
extension Data {
var attributedString: NSAttributedString? {
do {
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
return try NSAttributedString(data: self, options:options, documentAttributes: nil)
} catch {
print(error)
}
return nil
}
}
extension String {
var data: Data {
return Data(utf8)
}
}
...
txtIntro.attributedText = htmlStr.data.attributedString
谢谢,
奥斯曼
在 HTML 代码中的 <img>
标签周围创建一个 <center>
元素
<center><img src="..."/></center>
您可以将图像放在 div 中,将其样式设置为居中,将宽度设置为 100%
let htmlStr = """
<meta charset=utf-8>
<h3 style="text-align: center;">Sample text.</h3>
<div style="text-align: center; width: 100%;"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://cdn.sstatic.net/Sites/Whosebug/company/img/logos/so/so-logo.png?v=9c558ec15d8a" width="182" height="44"/></div>
"""
我正在尝试在我的 iOS 应用程序的 UITextView 中加载 html 内容。
我使用以下 html 代码:
<meta charset=utf-8>
<h3 style="text-align: center;">Sample text.</h3>
<p><img style="display: block; margin-left: auto; margin-right: auto;" src="https://cdn.sstatic.net/Sites/Whosebug/company/img/logos/so/so-logo.png?v=9c558ec15d8a" width="182" height="44"/></p>
所以它应该在中心显示文本和图像,但是图像没有居中:
有谁知道如何将图像居中对齐?
以下是我设置 html 内容的方法:
extension Data {
var attributedString: NSAttributedString? {
do {
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
return try NSAttributedString(data: self, options:options, documentAttributes: nil)
} catch {
print(error)
}
return nil
}
}
extension String {
var data: Data {
return Data(utf8)
}
}
...
txtIntro.attributedText = htmlStr.data.attributedString
谢谢, 奥斯曼
在 HTML 代码中的 <img>
标签周围创建一个 <center>
元素
<center><img src="..."/></center>
您可以将图像放在 div 中,将其样式设置为居中,将宽度设置为 100%
let htmlStr = """
<meta charset=utf-8>
<h3 style="text-align: center;">Sample text.</h3>
<div style="text-align: center; width: 100%;"><img style="display: block; margin-left: auto; margin-right: auto;" src="https://cdn.sstatic.net/Sites/Whosebug/company/img/logos/so/so-logo.png?v=9c558ec15d8a" width="182" height="44"/></div>
"""