I built a custom on-screen keyboard that's not an inputView
of the UITextField
In the delegate method from the keyboard, I want to call activeTextField.shouldChangeText(in: UITextRange, replacementText: String)
String
here is obviously the new value generated from my keyboard. UITextRange
is just a class meant to be subclassed:
/* 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 } }
I'm not exactly sure what to do here to target the full string. Any help would be appreciated. Thanks!
2 Answers
Answers 1
If I understand your question correctly then activeTextField
will already have implemented the subclasses for UITextPosition and UITextRange. All you need to do is use the appropriate getters to construct the UITextRange.
It sounds like you want to use the methods:
beginningOfDocument endOfDocument
and
textRange
Perhaps something like:
myTextRange = activeTextfield.textRange( activeTextfield.beginningOfDocument(), activeTextfield.endOfDocument())
The docs are here: https://developer.apple.com/documentation/uikit/uitextinput
Answers 2
First to answer your question directly you would create a text range like this:
let textRange = activeTextField.textRange(activeTextField.beginningOfDocument, activeTextField.endOfDocument)
this will create an option UITextRange so you need to take account of that.
However moving on even if you do that you are not going to be able to call the shouldChangeText
directly as it's part of the UITextInput protocol and is not implemented by default for a UITextField. You have to implement it yourself. Also that will only tell you if the text can be replaced it won't actually do the replacement.
0 comments:
Post a Comment