ios - 使用 setSelectedTextRange : on iOS 6 时的奇怪行为

标签 ios objective-c ios6 uitextfield uitextposition

我看到 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 上看到的:

Strange behavior

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

编辑

对于被设置为偏移量的每个非零值,这似乎都会发生。

最佳答案

编辑:全新的解决方案

终于找到问题所在了。基本上,据我所见,当您替换 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,从刚刚删除的字符之前的字符开始.

希望这对遇到类似问题的人有所帮助,我因此而发疯!

关于ios - 使用 setSelectedTextRange : on iOS 6 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397842/

相关文章:

ios - WKWebView的网络性能比原生safari慢吗?

objective-c - Objective C 中的 If (BOOL) : unused variable

iphone - pageviewcontroller 中的 PageCurlEffect 与工具栏重叠

iphone - 使用 coretelephony 捕获传入的 Callevent?

iOS:如何自定义JSQMessagesCollectionview?

ios - WebKit webview (WKwebview) 无法加载应用程序目录之外的资源

iphone - iOS 6 的 Paypal 问题

ios - 将图像上传到服务器需要很长时间

ios - 向类的实例发送 "class"消息是什么意思

objective-c - 从 XIB 加载 viewForSectionHeader 时遇到问题