带有未格式化表情符号的 NSAttributedString 结尾
End of NSAttributedString with emojis left unformatted
我的 NSAttributedString
内容中带有表情符号的结尾没有被格式化。我正在尝试格式化整个字符串(为简化起见,在此示例中将文本颜色设置为白色),但某些文本在放入 UILabel
.
时保持未格式化
目前,我正在使用
let attributedString = NSMutableAttributedString(string: contents)
attributedString.addAttribute(
NSForegroundColorAttributeName,
value: UIColor.white,
range: NSMakeRange(0, contents.characters.count)
)
label.attributedText = attributedString
我也尝试过使用 contents.utf8.count
获取长度,但得到的结果相同。
我注意到未格式化字符的数量与字符串中表情符号的数量相同。这与正在发生的事情有关系吗?
String.characters.count
returns the number of rendered characters in the string. Some emojis (such as flags and race-specific ones) are a combination of two or more UTF characters that are rendered into one character, in order to allow for more emojis.
UTF
stands for Unicode Transformation format, or Unicode for short. It allows for computers, phones, tablets, and everything else electronic to use the same standardized set of characters.
Those who implement it can chose how to render the text, but it is massively important that devices communicate using a standardized character set. Otherwise, sending the message "Hello, World" to someone may show up as "Ifmmp, Xpsme"
In order to get the actual length of the string for use in NSMakeRange
, use NSAttributedString.length
or Int("\(contents.endIndex)")
.
So, the code should look like this
let attributedString = NSMutableAttributedString(string: contents)
attributedString.addAttribute(
NSForegroundColorAttributeName,
value: UIColor.white,
range: NSMakeRange(0, attributedString.length)
)
label.attributedText = attributedString
This will now produce the correctly formatted text
我的 NSAttributedString
内容中带有表情符号的结尾没有被格式化。我正在尝试格式化整个字符串(为简化起见,在此示例中将文本颜色设置为白色),但某些文本在放入 UILabel
.
目前,我正在使用
let attributedString = NSMutableAttributedString(string: contents)
attributedString.addAttribute(
NSForegroundColorAttributeName,
value: UIColor.white,
range: NSMakeRange(0, contents.characters.count)
)
label.attributedText = attributedString
我也尝试过使用 contents.utf8.count
获取长度,但得到的结果相同。
我注意到未格式化字符的数量与字符串中表情符号的数量相同。这与正在发生的事情有关系吗?
String.characters.count
returns the number of rendered characters in the string. Some emojis (such as flags and race-specific ones) are a combination of two or more UTF characters that are rendered into one character, in order to allow for more emojis.
UTF
stands for Unicode Transformation format, or Unicode for short. It allows for computers, phones, tablets, and everything else electronic to use the same standardized set of characters.
Those who implement it can chose how to render the text, but it is massively important that devices communicate using a standardized character set. Otherwise, sending the message "Hello, World" to someone may show up as "Ifmmp, Xpsme"
In order to get the actual length of the string for use in NSMakeRange
, use NSAttributedString.length
or Int("\(contents.endIndex)")
.
So, the code should look like this
let attributedString = NSMutableAttributedString(string: contents)
attributedString.addAttribute(
NSForegroundColorAttributeName,
value: UIColor.white,
range: NSMakeRange(0, attributedString.length)
)
label.attributedText = attributedString
This will now produce the correctly formatted text