简体   繁体   English

如何在文本字段中找到字符串UITextInput创建(或长度)? (迅速)

[英]How do find the string UITextInput creates(or the length of) in a text field? (swift)

I'm trying to delete an entire word in a text field when I do a long press on the delete key. 当我长按删除键时,我试图删除文本字段中的整个单词。

I attempted to run a while loop to check for a space (" "), and then delete any char that did not match space (" ") - but... 我试图运行while循环来检查空格(“”),然后删除任何与空格不匹配的字符(“”) - 但是......

1) I am not sure if how I'm attempting to search the text field is correct 1)我不确定我是如何尝试搜索文本字段的

2) The way I'm attempting my loop is broken because of that 2)因为这个原因,我试图循环的方式被打破了

func delLong(){
    var proxy = textDocumentProxy as UITextDocumentProxy
    while [-1] != " "{
        proxy.deleteBackward()
    }
}

I haven't tested, but you can try: 我没有测试过,但你可以尝试:

func delLong(){
    var proxy = textDocumentProxy as UITextDocumentProxy
    while proxy.hasText() {
        let beforeText = proxy.documentContextBeforeInput
        if beforeText.isEmpty || beforeText.hasSuffix(" ") {
            break
        }
        proxy.deleteBackward()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM