为什么我转换后的 html 字符串只显示第一行?
Why does my converted html string only show the first line?
我已经尝试将 html
代码转换为 attributed string
然后在标签中使用。但出于某种原因,它只显示第一行:“在继续之前:”其他一切都是空白的。我已经扩展了 label
约束以填满整个屏幕,以确保没有问题。此 html
代码段已经针对 android
进行了测试并且工作正常。
let mystring = """
<body>
<p><b>Before proceeding:</b></p>
<ul type="dash">
<li>Save a backup copy of the door configuration file</li><li>Disconnect all other devices</li></ul>
<p><b>During upgrade:</b></p>
<ul type="dash">
<li>Do not turn off phone</li>
</ul>
<p><b>Are you sure you want to upgrade?</b></p>
</body>
"""
var attrString: NSAttributedString? {
guard let data = mystring.data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
mylabel.attributedText = attrString
有什么我遗漏的吗?
可能不是很明显,但是...
乍一看,当文本足够长时,UILabel
的 Number of Lines
属性 似乎允许自动换行。
然而...
UILabel
的 Number of Lines
属性 也控制显示的行数,即使文本中嵌入了换行符。
设置 mylabel.numberOfLines = 0
将解决此问题。
我已经尝试将 html
代码转换为 attributed string
然后在标签中使用。但出于某种原因,它只显示第一行:“在继续之前:”其他一切都是空白的。我已经扩展了 label
约束以填满整个屏幕,以确保没有问题。此 html
代码段已经针对 android
进行了测试并且工作正常。
let mystring = """
<body>
<p><b>Before proceeding:</b></p>
<ul type="dash">
<li>Save a backup copy of the door configuration file</li><li>Disconnect all other devices</li></ul>
<p><b>During upgrade:</b></p>
<ul type="dash">
<li>Do not turn off phone</li>
</ul>
<p><b>Are you sure you want to upgrade?</b></p>
</body>
"""
var attrString: NSAttributedString? {
guard let data = mystring.data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
mylabel.attributedText = attrString
有什么我遗漏的吗?
可能不是很明显,但是...
乍一看,当文本足够长时,UILabel
的 Number of Lines
属性 似乎允许自动换行。
然而...
UILabel
的 Number of Lines
属性 也控制显示的行数,即使文本中嵌入了换行符。
设置 mylabel.numberOfLines = 0
将解决此问题。