QuickType预测采用关键笔划,应该被我的UITextFieldDelegate阻止 [英] QuickType predictions take key strokes into account that should be blocked by my UITextFieldDelegate

查看:137
本文介绍了QuickType预测采用关键笔划,应该被我的UITextFieldDelegate阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textField,其中我不想允许前导空白。所以我实现了 textField(textField:shouldChangeCharactersInRange:replacementString:)和阻止尝试,这将改变文本为以空格开头的东西。这个工作正常。

I have a textField, in which I don't want to allow leading whitespace. So I implemented textField(textField:shouldChangeCharactersInRange:replacementString:) and block attempts which would change the text to something that starts with whitespace. This works as expected.

不幸的是,这会弄乱QuickType。每次我在空字段中按空格,然后我的textField忽略空格,Quicktype文本将以此空格为前缀。为了更清楚,QuickType将插入get的文本前缀,额外的空格不会显示在QuickType栏的UI中。

Unfortunately this messes up QuickType. Each time I press space in an empty field, which is then ignored by my textField, the Quicktype text gets prefixed with this space. To be more clear, the text that QuickType will insert get's prefixed, the additional whitespace is not shown in the UI of the QuickType bar.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text as NSString
    let proposedText = currentText.stringByReplacingCharactersInRange(range, withString: string)
    if !proposedText.isEmpty {
        let firstCharacterString = String(proposedText[proposedText.startIndex]) as NSString
        if firstCharacterString.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).location == 0 {
            println("starts with whitespace \"\(proposedText)\"")
            return false
        }
    }
    return true
}

这是一些日志记录,当我按下空格5次,然后按我是 QuickType建议: >

Here is some logging to see what's happening when I press space 5 times followed by press on the I'm QuickType suggestion:

starts with whitespace " "           // pressing space key
starts with whitespace " "
starts with whitespace " "
starts with whitespace " "
starts with whitespace " "
starts with whitespace "     I'm"    // Inserted by QuickType after pressing the "I'm" suggestion
starts with whitespace " "           // Inserted by QuickType as well

通过检查委托方法中的变量,问题确实是与替换字符串,我从UITextField。它已经包含以空格前缀的建议。

By inspecting the variables in that delegate method I can verify that the problem is indeed with the replacement string, that I get from the UITextField. It already contains the suggestion prefixed with whitespace.

有人知道我可以如何防止这种情况,或者如何重置QuickType建议?

Does anybody know how I can prevent that, or how I can "reset" QuickType suggestions?

解决方法是从多字符插入中修剪空格,但首先我想看看是否缺少一种以干净的方式处理问题的方法。

A workaround would be to trim whitespace from multi character inserts, but first I want to see if I'm missing a way to handle the problem in a clean way.

推荐答案

有了更多的测试,我得出结论,这是一个错误。

With some more testing I came to the conclusion that this is a bug.

首先,我认为Keyboard和QuickType与UITextField解耦。但实际上不是这样的。在填充的textField中更改光标的位置实际上会更改quicktype建议。因此,textField实际上与quicktype进行通信。

First I thought that Keyboard and QuickType are decoupled from the UITextField. But this is actually not the case. Changing the position of the cursor in a filled textField actually changes quicktype suggestions. So the textField actually communicates with quicktype.

所以我提交了一个错误。

So I filed a bug.

Apple上的错误:rdar:// 19250739

OpenRadar上的错误: 5794406481264640

Bug at Apple: rdar://19250739
Bug at OpenRadar: 5794406481264640

如果有人对解决方法感兴趣:

In case somebody is interested in the workaround:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text as NSString
    let proposedText = currentText.stringByReplacingCharactersInRange(range, withString: string)

    if proposedText.hasPrefix(" ") {
        // Don't allow space at beginning
        println("Ignore text \"\(proposedText)\"")

        // workaround
        if textField.text == "" && countElements(string) > 1 {
            // multi insert into empty field. probably from QuickType
            // so we should strip whitespace and set new text directly
            let trimmedString = proposedText.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            println("Inserted \"\(trimmedString)\" with Quicktype. ")
            textField.text = trimmedString
        }

        return false
    }
    return true
}

这篇关于QuickType预测采用关键笔划,应该被我的UITextFieldDelegate阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆