【问题标题】:Using a subclass of UITextView and a custom on-screen keyboard, calling textField.textShouldChange使用 UITextView 的子类和自定义屏幕键盘,调用 textField.textShouldChange
【发布时间】:2018-01-01 20:21:36
【问题描述】:

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

在键盘的委托方法中,我想调用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 }
}

我不完全确定要在这里做什么来定位完整的字符串。任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: ios swift uitextfield uikit


    【解决方案1】:

    如果我正确理解了您的问题,那么activeTextField 将已经实现了 UITextPosition 和 UITextRange 的子类。您需要做的就是使用适当的 getter 来构造 UITextRange。

    听起来你想使用方法:

    beginningOfDocument
    
    endOfDocument
    

    textRange
    

    可能是这样的:

    myTextRange = activeTextfield.textRange(
                    activeTextfield.beginningOfDocument(), 
                    activeTextfield.endOfDocument())
    

    文档在这里:https://developer.apple.com/documentation/uikit/uitextinput

    【讨论】:

      【解决方案2】:

      首先要直接回答您的问题,您将创建一个像这样的文本范围:

      let textRange = activeTextField.textRange(activeTextField.beginningOfDocument, activeTextField.endOfDocument)
      

      这将创建一个选项 UITextRange,因此您需要考虑到这一点。

      但是,即使你这样做了,你也不能直接调用shouldChangeText,因为它是 UITextInput 协议的一部分,并且默认情况下不为 UITextField 实现。你必须自己实现它。此外,这只会告诉您文本是否可以替换它实际上不会进行替换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-23
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        • 2019-02-07
        相关资源
        最近更新 更多