简体   繁体   English

在iOS 6上使用setSelectedTextRange:时出现奇怪的行为

[英]Strange behavior when using setSelectedTextRange: on iOS 6

I'm seeing some very strange behavior with a UITextField . 我看到UITextField有一些非常奇怪的行为。 I have a custom keyboard implemented, and it works great on iOS 7+. 我实现了一个自定义键盘,它在iOS 7+上运行良好。 However, on iOS 6, when the following line is called (after doing a "backspace"), the text field's cursor vanishes, it isn't editable without resigning and becoming the first responder again, and the placeholder and real text overlap. 但是,在iOS 6上,当调用以下行(执行“退格”后)时,文本字段的光标消失,如果不退出并再次成为第一响应者,则不可编辑,并且占位符和真实文本重叠。

Here's the relevant code for my "backspace" function: 这是我的“退格”功能的相关代码:

//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
}

And here's what I'm seeing on iOS 6: 这是我在iOS 6上看到的内容:

奇怪的行为

Anyone have any insights on this? 有人对此有见解吗? Thanks! 谢谢!

EDIT 编辑

This appears to be happening for every nonzero value being set as the offset . 对于每个设置为offset非零值,似乎正在发生这种情况。

EDIT: Completely new solution 编辑:全新的解决方案

Finally figured out the problem. 终于解决了问题。 Basically, from what I've seen, when you replace the text of a UITextField , it resets the selectedTextRange to the very end of the string (which makes sense since that's where the cursor is). 基本上,从我所看到的,当您替换UITextFieldtext时,它会将selectedTextRange重置为字符串的末尾(这很有意义,因为这是光标所在的位置)。 With this in mind, I was able to come up with the following code which works on iOS 6 and 7. 考虑到这一点,我能够提出以下适用于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];
}

Essentially what's happening is it's creating a UITextRange from a UITextPosition with length of 0 starting from the character before the character that was just deleted. 从本质上讲, UITextRangeUITextPosition创建一个UITextRange ,长度为0从刚删除的字符之前的字符开始。

Hope this helps someone with a similar problem, I was going insane because of this! 希望这对遇到类似问题的人有所帮助,因此我疯了!

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

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