我构建了一个自定义屏幕键盘,它不是 UITextField
的 inputView
在键盘的委托(delegate)方法中,我想调用 activeTextField.shouldChangeText(in: UITextRange, replacementText: String)
String
这里显然是我的键盘生成的新值。 UITextRange
只是一个要被子类化的类:
/* To accommodate text entry in documents that contain nested elements, or in which supplying and
* evaluating characters at indices is an expensive proposition, a position within a text input
* document is represented as an object, not an integer. UITextRange and UITextPosition are abstract
* classes provided to be subclassed when adopting UITextInput */
@available(iOS 3.2, *)
open class UITextRange : NSObject {
open var isEmpty: Bool { get } // Whether the range is zero-length.
open var start: UITextPosition { get }
open var end: UITextPosition { get }
}
我不确定在这里要做什么来定位完整的字符串。任何帮助,将不胜感激。谢谢!
最佳答案
如果我正确理解您的问题,那么 activeTextField
将已经实现了 UITextPosition 和 UITextRange 的子类。您需要做的就是使用适当的 getter 来构造 UITextRange。
听起来您想使用这些方法:
beginningOfDocument
endOfDocument
和
textRange
也许是这样的:
myTextRange = activeTextfield.textRange(
activeTextfield.beginningOfDocument(),
activeTextfield.endOfDocument())
文档在这里:https://developer.apple.com/documentation/uikit/uitextinput
关于ios - 使用 UITextView 的子类和自定义屏幕键盘,调用 textField.textShouldChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48052957/