【问题标题】:Strange behavior when using setSelectedTextRange: on iOS 6使用 setSelectedTextRange 时的奇怪行为:在 iOS 6 上
【发布时间】:2014-10-13 09:43:22
【问题描述】:

我看到UITextField 出现了一些非常奇怪的行为。我实现了一个自定义键盘,它在 iOS 7+ 上运行良好。但是,在 iOS 6 上,当调用以下行时(在执行“退格”之后),文本字段的光标消失,如果不辞职并再次成为第一响应者,则无法编辑,并且占位符和实际文本重叠。

这是我的“退格”功能的相关代码:

//Get the position of the cursor
UITextPosition *selStartPos = self.textBeingEdited.selectedTextRange.start;
int start = (int)[self.textBeingEdited offsetFromPosition:self.textBeingEdited.beginningOfDocument toPosition:selStartPos];

//Make sure the cursor isn't at the front of the document
if (start > 0) {

    //Remove the character before the cursor
    self.textBeingEdited.text = [NSString stringWithFormat:@"%@%@", [self.textBeingEdited.text substringToIndex:start - 1], [self.textBeingEdited.text substringFromIndex:start]];

    //Move the cursor back 1 (by default it'll go to the end of the string for some reason)
    [self.textBeingEdited setSelectedTextRange:[self.textBeingEdited textRangeFromPosition:[self.textBeingEdited positionFromPosition:selStartPos offset:-1] toPosition:[self.textBeingEdited positionFromPosition:selStartPos offset:-1]]];
    //^This line is causing the issue
}

这就是我在 iOS 6 上看到的:

有人对此有任何见解吗?谢谢!

编辑

对于每个设置为 offset 的非零值似乎都会发生这种情况。

【问题讨论】:

    标签: ios objective-c ios6 uitextfield uitextposition


    【解决方案1】:

    编辑:全新的解决方案

    终于找到问题所在了。基本上,据我所见,当您替换UITextFieldtext 时,它会将selectedTextRange 重置为字符串的最末尾(这是有道理的,因为那是光标所在的位置)。考虑到这一点,我能够提出以下适用于 iOS 6 和 7 的代码。

    //Get the position of the cursor
    UITextPosition *startPosition = self.textBeingEdited.selectedTextRange.start;
    int start = (int)[self.textBeingEdited offsetFromPosition:self.textBeingEdited.beginningOfDocument toPosition:startPosition];
    
    //Make sure the cursor isn't at the front of the document
    if (start > 0) {
    
        //Remove the character before the cursor
        self.textBeingEdited.text = [NSString stringWithFormat:@"%@%@", [self.textBeingEdited.text substringToIndex:(start - 1)], [self.textBeingEdited.text substringFromIndex:start]];
        //Note that this line ^ resets the selected range to just the very end of the string
    
        //Get the position from the start of the text to the character deleted's index - 1
        UITextPosition *position = [self.textBeingEdited positionFromPosition:self.textBeingEdited.beginningOfDocument offset:(start - 1)];
    
        //Create a new range with a length of 0
        UITextRange *newRange = [self.textBeingEdited textRangeFromPosition:position toPosition:position];
    
        //Update the cursor position (selected range with length 0)
        [self.textBeingEdited setSelectedTextRange:newRange];
    }
    

    基本上发生的事情是它从UITextPosition 创建一个UITextRange,长度为0,从刚刚删除的字符之前的字符开始。

    希望这可以帮助遇到类似问题的人,因为这个我快疯了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      相关资源
      最近更新 更多