【问题标题】:UITextViewDelegate behaviour when backspace key is HELD Down退格键按下时的 UITextViewDelegate 行为
【发布时间】:2011-08-25 20:46:33
【问题描述】:

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

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

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

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

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

[textView substringWithRange:range]
string contains " "

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

【问题讨论】:

  • 这个西蒙运气好吗?有没有办法防止“逐字删除”动作或其他什么?
  • 我仍然找不到阻止它的方法(或让它向委托人提交正确的数据),但能够检测到它使我能够解决这个错误。跨度>
  • 请注意,UITextViewDelegate 还有一个额外的错误。如果您在设置中将 General->Accessibility->Triple-click Home 设置为“VoiceOver”或“Ask”,则 UITextViewDelegates 会收到任何标点字符的重复“shouldChangeTextInRange”消息。这很难追踪。
  • 如果只注册更改,为什么还要同时使用 shouldChange 和 didChange?
  • 因为 shouldChange 给了我增量变化,它允许更快地更新内部缓冲区。 didChange 需要我分析文本中的差异。

标签: iphone cocoa-touch ios uitextview uitextviewdelegate


【解决方案1】:

Jacob 提到我应该将其发布为答案。就是这样。

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

- (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];
    } 
}

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多