UITextView 中 UTF16 字符的 NSAttributedString
NSAttributedString from UTF16 characters in UITextView
我有以下代码为 UITextView
中的字符串创建超链接
extension UITextView {
func setAttributed(text: String) {
self.attributedText = text.htmlAttributedString()
}
}
extension String {
func htmlAttributedString(document: NSAttributedString.DocumentType = .html) -> NSAttributedString? {
guard let data = self.data(using: .utf16, allowLossyConversion: false), let font = font else { return nil }
do {
return try NSMutableAttributedString(data: data, options: [.documentType: document, .characterEncoding: String.Encoding.utf16.rawValue], documentAttributes: nil)
} catch {
debugger("error in string conversion. \(error.localizedDescription)")
return nil
}
}
调用方式如下:self.messageTextView.setAttributed(text: "<a href=\"\(utf16_str1)\">\(utf16_str2)</a>")
当 text
仅包含简单的 utf8 字符和 returns 以下结果时,它工作正常:
text {
NSColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSFont = "<UICTFont: 0x1056bd910> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSLink = "https://google.com";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSStrokeWidth = 0;
NSUnderline = 1;
}
但是,当 text
包含 utf16 字符时,例如 ä 或 ö,结果分解如下:
<a href="https://google.com">textäö</a>{
NSFont = "<UICTFont: 0x109f0eb30> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
我该如何解决这个问题,这样带有 ä 等字符的字符串不会破坏结果
显然,在此类用例之前,必须对具有 non-ascii 个字符的字符串进行规范化。使用 precomposedStringWithCanonicalMapping
解决了此问题,如下所述:https://developer.apple.com/documentation/foundation/nsstring/1412645-precomposedstringwithcanonicalma.
我有以下代码为 UITextView
extension UITextView {
func setAttributed(text: String) {
self.attributedText = text.htmlAttributedString()
}
}
extension String {
func htmlAttributedString(document: NSAttributedString.DocumentType = .html) -> NSAttributedString? {
guard let data = self.data(using: .utf16, allowLossyConversion: false), let font = font else { return nil }
do {
return try NSMutableAttributedString(data: data, options: [.documentType: document, .characterEncoding: String.Encoding.utf16.rawValue], documentAttributes: nil)
} catch {
debugger("error in string conversion. \(error.localizedDescription)")
return nil
}
}
调用方式如下:self.messageTextView.setAttributed(text: "<a href=\"\(utf16_str1)\">\(utf16_str2)</a>")
当 text
仅包含简单的 utf8 字符和 returns 以下结果时,它工作正常:
text {
NSColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSFont = "<UICTFont: 0x1056bd910> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSLink = "https://google.com";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 15/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0.933333 1 ";
NSStrokeWidth = 0;
NSUnderline = 1;
}
但是,当 text
包含 utf16 字符时,例如 ä 或 ö,结果分解如下:
<a href="https://google.com">textäö</a>{
NSFont = "<UICTFont: 0x109f0eb30> font-family: \".SFUI-Regular\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}
我该如何解决这个问题,这样带有 ä 等字符的字符串不会破坏结果
显然,在此类用例之前,必须对具有 non-ascii 个字符的字符串进行规范化。使用 precomposedStringWithCanonicalMapping
解决了此问题,如下所述:https://developer.apple.com/documentation/foundation/nsstring/1412645-precomposedstringwithcanonicalma.