iOS中键盘输入的字符串值是什么编码?

What is the encoding of String value that is input from the keyboard in iOS?

我有以下代码:

let range = NSRange(location: 0, length: mutableAttributedString.string.lengthOfBytes(using: String.Encoding))
mutableAttributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body), range: range)

我需要知道在上面代码的第一行中参数 "using" 的 "String.Encoding" 处使用什么字符串编码。 String 对象总是来自 iOS 键盘。

如果您希望创建一个 NSRange 用于添加属性,我认为 lengthOfBytes 不是您要找的东西。这里的范围表示原始 mutableAttributedString 中的一个子字符串,如果您只是想向整个字符串添加一个属性,则您的范围应定义为:

let range = NSRange(location: 0, length: mutableAttributedString.string.count)

否则我不确定您要使用 lengthOfBytes 做什么,但我认为这不会像您在这里所做的那样添加属性。