ios - 使用 UITextView 的子类和自定义屏幕键盘,调用 textField.textShouldChange

标签 ios swift uitextfield uikit

我构建了一个自定义屏幕键盘,它不是 UITextFieldinputView

在键盘的委托(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/

相关文章:

ios - 在 ios 的谷歌地图中使用他的坐标获取特定标记

ios - 从 iOS Swift 中的 xml 文本文件中提取 Json

ios - 如果禁用 UITextField(在相应的 UIView 中),更改 UILabel 的颜色? ( swift )

ios - 自动向下滚动到下一个 UITextField

ios - 后台获取和本地通知

ios - 如何像 Apple 一样突出显示 UIView

ios - 如何在循环内对 UIView 进行动画 n 次并跟踪每个动画事件

swift - 将带有 IBAction 的 View 添加到 NSStackView 会导致应用程序崩溃

swift - 使用 Xcode 9 构建的 macOS 应用程序可以在 10.13 之前的 macOS 版本上运行吗?

ios - 在左上角位置的边框上带有标签的 TextField