gpt4 book ai didi

iphone - 按住退格键时的 UITextViewDelegate 行为

转载 作者:可可西里 更新时间:2023-11-01 05:42:38 24 4
gpt4 key购买 nike

我遇到了一个问题,即在键盘上按住删除键时,iOS 会向我的 UITextViewDelegate 提供不正确的信息。

当用户按住 iPad 上的 UITextView 上的删除键时,UITextView 将开始删除整个单词而不是单个字符,按住的时间越长(注意:这不会发生在模拟器)。

发生这种情况时,UITextView 委托(delegate)方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

使用包含正确光标位置但长度为 1 的范围进行调用。这是不正确的,因为 UITextView 现在正在删除整个单词,而不是单个字母。例如,下面的代码将只打印一个空格。

[textView substringWithRange:range]
string contains " "

尽管 UITextView 删除了整个单词。替换文本作为空字符串正确给出。有谁知道这个问题的解决方案或解决方法吗?

最佳答案

Jacob 提到我应该将此作为答案发布。所以就在这里。

我的变通方法是监控 shouldChangeTextInRange 中给定的文本长度和范围,然后将其与 textViewDidChange 中的文本长度进行比较。如果差异不同步,我会刷新我的支持文本缓冲区并从 TextView 中重建它。这不是最佳的。这是我的临时解决方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//Push the proposed edit to the underlying buffer
[self.editor.buffer changeTextInRange:range replacementText:text];

//lastTextLength is an NSUInteger recording the length that
//this proposed edit SHOULD make the text view have
lastTextLength = [textView.text length] + ([text length] - range.length);

return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
//Check if the lastTextLength and actual text length went out of sync
if( lastTextLength != [textView.text length] )
{
//Flush your internal buffer
[self.editor.buffer loadText:textView.text];
}
}

关于iphone - 按住退格键时的 UITextViewDelegate 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com