通过覆盖shouldChangeText获取新字符串(范围:UITextRange,replacementText文本:String)

时间:2019-01-08 15:15:48

标签: swift uitextfield

我有一个自定义的UITextField类,我想在其中覆盖

override func shouldChangeText(in range: UITextRange, replacementText text: String) -> Bool

但是我无法获得新的更改字符串

override func shouldChangeText(in range: UITextRange, replacementText text: String) -> Bool {

    if (self.changeHandler != nil) {

        let location = offset(from: beginningOfDocument, to: range.start)
        let length = offset(from: range.start, to: range.end)
        let r = Range(NSRange(location: location, length: length))
        let newText = self.text!.replacingCharacters(in: r!, with: text)
        return self.changeHandler!(newText)

    }
    return super.shouldChangeText(in: range, replacementText: text)

}

我明白了 对成员“文本”的含糊不清

0 个答案:

没有答案