Swift: 空 space 添加同时将 html 字符串转换为属性字符串
Swift: Empty space adding while converting html string to attribute string
正在从 API
接收 HTML
类型 String
。
正在将 HTML
转换为 NSAttributeString
--> Unicode
面临空白 Space 问题 - \u200b
如何把这个白色的space从HTML
去掉到NSAttributeString
?
API 响应:
{
"message" : "[<b>1-</b> Fill\U200b t\U200bhe \U200b\U200b\U200bBreak Do\U200bwn Table]"
}
结构-解码器:
{
"message" : "[<b>1-</b> Fill the Break Down Table]"
}
UILabel 文本:
labet.text = Message.message // [<b>1-</b> Fill the Break Down Table] - SHOWING IN UI
UILabel 属性文字:
labet.attributedText = Message.message.htmlAttributedString()
属性输出:
- SHOWING IN UI
分机号:[=57=]
func htmlAttributedString() -> NSMutableAttributedString {
guard let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false)
else { return NSMutableAttributedString() }
guard let formattedString = try? NSMutableAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil )
else { return NSMutableAttributedString() }
return formattedString
}
任何人都可以指导我吗?如何避免这种情况 space ?
试试这个:
labet.attributedText = Message.message.replacingOccurrences(of: "\u{200B}", with: "").htmlAttributedString()
正在从 API
接收 HTML
类型 String
。
正在将 HTML
转换为 NSAttributeString
--> Unicode
面临空白 Space 问题 - \u200b
如何把这个白色的space从HTML
去掉到NSAttributeString
?
API 响应:
{
"message" : "[<b>1-</b> Fill\U200b t\U200bhe \U200b\U200b\U200bBreak Do\U200bwn Table]"
}
结构-解码器:
{
"message" : "[<b>1-</b> Fill the Break Down Table]"
}
UILabel 文本:
labet.text = Message.message // [<b>1-</b> Fill the Break Down Table] - SHOWING IN UI
UILabel 属性文字:
labet.attributedText = Message.message.htmlAttributedString()
属性输出:
- SHOWING IN UI
分机号:[=57=]
func htmlAttributedString() -> NSMutableAttributedString {
guard let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false)
else { return NSMutableAttributedString() }
guard let formattedString = try? NSMutableAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil )
else { return NSMutableAttributedString() }
return formattedString
}
任何人都可以指导我吗?如何避免这种情况 space ?
试试这个:
labet.attributedText = Message.message.replacingOccurrences(of: "\u{200B}", with: "").htmlAttributedString()