简体   繁体   English

从iOS的键盘输入的String值的编码是什么?

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

I have the following code: 我有以下代码:

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)

I need to know what string encoding to use where it says "String.Encoding" for the parameter "using" in the first line of the code above. 我需要知道在上面代码的第一行中对于参数“ using”说“ String.Encoding”的地方使用哪种字符串编码。 The String object would always originate from the iOS keyboard. String对象始终来自iOS键盘。

If you are looking to create an NSRange to be used to add attributes, I don't believe lengthOfBytes is what you're looking for. 如果要创建用于添加属性的NSRange ,我不认为lengthOfBytes是您要的内容。 Here a range represents a substring within your original mutableAttributedString , if you are simply looking to add an attribute to the whole string, your range should be defined as: 这里的范围代表原始mutableAttributedString的子字符串,如果您只是想向整个字符串添加属性,则范围应定义为:

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

I'm otherwise not sure what you'd be using lengthOfBytes for, but I don't think that'll work for adding attributes as you seem to be doing here. 否则我不确定您将使用lengthOfBytes做什么,但是我认为这似乎无法在您添加属性时起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM