Skip to main content
 首页 » 操作系统

ios之如何在文本字段中找到 UITextInput 创建的字符串(或长度) ( swift )

2024年01月01日49zhoujg

当我长按删除键时,我试图删除文本字段中的整个单词。

我尝试运行一个 while 循环来检查空格 (""),然后删除任何与空格 ("") 不匹配的字符 - 但是...

1) 我不确定我尝试搜索文本字段的方式是否正确

2) 因此,我尝试循环的方式被打破了

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

请您参考如下方法:

我没有测试过,但你可以试试:

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