iOS 13 中的属性字符串问题
attributed string issue in iOS 13
我正在使用以下扩展将 HTML 字符串转换为属性字符串,以呈现我正在使用 UILabel
的属性字符串。字体系列是 system-regular,iPhone/iPad 的点数为 15/18。在上面的图片中,Description 是标题,属性字符串是值。
extension NSAttributedString {
internal convenience init?(html: String, font : UIFont) {
let modifiedFont = String(format:"<span style=\"font-family: '-apple-system', '\(font.fontName)'; font-size: \(font.pointSize)\">%@</span>", html)
guard let data = modifiedFont.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
return nil
}
guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
return nil
}
self.init(attributedString: attributedString)
}
}
上述扩展中变量的控制台打印
(lldb) po html
"<p><font size=\"2\">Pack Size: 2.5 Gal x 2</font></p><p><font size=\"2\">Gallons per case / bucket / barrel:</font></p><p><font size=\"2\">1 Case – 5.0 Gal<br> 2 Cases – 10.0 Gal<br> 3 Cases – 15.0 Gal</font></p>"
(lldb) po font
<UICTFont: 0x7ffbe4b03a20> font-family: ".SFUI-Regular"; font-weight: normal; font-style: normal; font-size: 15.00pt
(lldb) po modifiedFont
"<span style=\"font-family: \'-apple-system\', \'.SFUI-Regular\'; font-size: 15.0\"><p><font size=\"2\">Pack Size: 2.5 Gal x 2</font></p><p><font size=\"2\">Gallons per case / bucket / barrel:</font></p><p><font size=\"2\">1 Case – 5.0 Gal<br> 2 Cases – 10.0 Gal<br> 3 Cases – 15.0 Gal</font></p></span>"
我正在呼叫如下分机
self.lblValueDescription.attributedText = NSAttributedString(html: data.descriptionField,
font: self.lblValueDescription.font)
输出在iOS12
输出在iOS13
问题:
- 字体大小与 iOS12
中预期的不同
- iOS13 有一些具有相同 html 字符串的外来字符。
这有点 encode/decode 不匹配的味道。您明确指定 utf16
用于数据编码。但是,由于 this,您的编码可能存在冲突。这也可以解释乱码和 iOS 12 vs 13 问题。
试试这个解决方案,它对我这边的 ios 12 和 13 都有效,可能对你有帮助。
extension String
{
func htmlAttributed(family: String?, size: CGFloat, colorHex: String) -> NSAttributedString?
{
do {
let htmlCSSString = "<style>" +
"html *" +
"{" +
"font-size: \(size)pt !important;" +
"color: " + "\(colorHex)" + " !important;" +
"font-family: \(family ?? "Helvetica"), Helvetica !important;" +
"}</style> \(self)"
guard let data = htmlCSSString.data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
}
并通过此行设置文本
self.lblValueDescription.attributedText = yourHtmlString.htmlAttributed(family: "Roboto-Regular", size: 11,colorHex: "#000000")!
Edit:
查看我在下面所做的答案后更改了
let modifiedFont = String(format:"<style>html *{font-family: '-apple-system', '\(font.fontName)' !important; font-size: \(font.pointSize) !important}</style>%@", html)
- 用样式块修改样式标签
!important
添加标志以强制应用样式。
我正在使用以下扩展将 HTML 字符串转换为属性字符串,以呈现我正在使用 UILabel
的属性字符串。字体系列是 system-regular,iPhone/iPad 的点数为 15/18。在上面的图片中,Description 是标题,属性字符串是值。
extension NSAttributedString {
internal convenience init?(html: String, font : UIFont) {
let modifiedFont = String(format:"<span style=\"font-family: '-apple-system', '\(font.fontName)'; font-size: \(font.pointSize)\">%@</span>", html)
guard let data = modifiedFont.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
return nil
}
guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
return nil
}
self.init(attributedString: attributedString)
}
}
上述扩展中变量的控制台打印
(lldb) po html
"<p><font size=\"2\">Pack Size: 2.5 Gal x 2</font></p><p><font size=\"2\">Gallons per case / bucket / barrel:</font></p><p><font size=\"2\">1 Case – 5.0 Gal<br> 2 Cases – 10.0 Gal<br> 3 Cases – 15.0 Gal</font></p>"
(lldb) po font
<UICTFont: 0x7ffbe4b03a20> font-family: ".SFUI-Regular"; font-weight: normal; font-style: normal; font-size: 15.00pt
(lldb) po modifiedFont
"<span style=\"font-family: \'-apple-system\', \'.SFUI-Regular\'; font-size: 15.0\"><p><font size=\"2\">Pack Size: 2.5 Gal x 2</font></p><p><font size=\"2\">Gallons per case / bucket / barrel:</font></p><p><font size=\"2\">1 Case – 5.0 Gal<br> 2 Cases – 10.0 Gal<br> 3 Cases – 15.0 Gal</font></p></span>"
我正在呼叫如下分机
self.lblValueDescription.attributedText = NSAttributedString(html: data.descriptionField,
font: self.lblValueDescription.font)
输出在iOS12
输出在iOS13
问题:
- 字体大小与 iOS12 中预期的不同
- iOS13 有一些具有相同 html 字符串的外来字符。
这有点 encode/decode 不匹配的味道。您明确指定 utf16
用于数据编码。但是,由于 this,您的编码可能存在冲突。这也可以解释乱码和 iOS 12 vs 13 问题。
试试这个解决方案,它对我这边的 ios 12 和 13 都有效,可能对你有帮助。
extension String
{
func htmlAttributed(family: String?, size: CGFloat, colorHex: String) -> NSAttributedString?
{
do {
let htmlCSSString = "<style>" +
"html *" +
"{" +
"font-size: \(size)pt !important;" +
"color: " + "\(colorHex)" + " !important;" +
"font-family: \(family ?? "Helvetica"), Helvetica !important;" +
"}</style> \(self)"
guard let data = htmlCSSString.data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
}
并通过此行设置文本
self.lblValueDescription.attributedText = yourHtmlString.htmlAttributed(family: "Roboto-Regular", size: 11,colorHex: "#000000")!
Edit:
查看我在下面所做的答案后更改了
let modifiedFont = String(format:"<style>html *{font-family: '-apple-system', '\(font.fontName)' !important; font-size: \(font.pointSize) !important}</style>%@", html)
- 用样式块修改样式标签
!important
添加标志以强制应用样式。