UITextView selectAll方法无法按预期工作 [英] UITextView selectAll method not working as expected

查看:177
本文介绍了UITextView selectAll方法无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的iPhone 5(其上安装了iOS 8.0.2)创建一个带有Xcode 6.0.1的iOS 8应用程序。我想这样做,以便当用户点击我的 UITextView 时,所有文本都被选中,这样他就可以轻松地开始输入并删除那里的内容(但我没有希望文本被自动删除,因为用户可能想要保留它或附加到它上面。为此,我有以下代码:

I'm creating an iOS 8 app with Xcode 6.0.1 for my iPhone 5 (which has iOS 8.0.2 on it). I want to make it so that when a user clicks on my UITextView, all the text gets selected so he can easily start typing and erase what was there (but I don't want the text to be automatically erased because the user may want to keep it or append to it). To do this, I have the following code:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView hasText]) {
        NSLog(@"selectedRange before: %d", textView.selectedRange.length);
        [textView selectAll:self];
        NSLog(@"selectedRange after: %d", textView.selectedRange.length);
    }
}

调用此方法时,控制台输出是什么我期望(即 selectedRange 长度与 textView 的文本中的字符数相同)。但是,在 UITextView 中没有显示任何内容,并且它没有被选中(即没有弹出选择菜单)。

When this method gets called, the console output is what I expect (i.e. the selectedRange length is the same as the number of characters in the textView's text). However, nothing shows up as selected in the UITextView and it doesn't act selected (i.e. no selection menu pops up).

我在互联网上看到过这样的多个问题,但是所提供的解决方案都没有对我有用(有些人在没有提供任何解决方案的情况下把它写成了一个bug)。将发件人ID更改为 self 以外的其他内容(例如 nil )没有帮助,也没有帮助如一个人建议的那样,打电话 [textView select:self] 。我也试过这段代码:

I have seen multiple questions like this on the internet, but none of the provided solutions worked for me (and some of them wrote it off as a bug without providing any solution). Changing the sender id to something other than self (such as nil) did not help, and neither did it help to call [textView select:self] as one person suggested. I have also tried this code:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView hasText]) {
        UITextRange *range = [textView textRangeFromPosition:textView.beginningOfDocument toPosition:textView.endOfDocument];
        [textView setSelectedTextRange:range];
    }
}

但是,它有同样的问题。

But, it has the same problem.

有任何建议吗?

推荐答案

我发现这个问题的最佳解决方案到目前为止是创建一个自定义 UITextView (即创建一个扩展 UITextView 的新类),然后实现 selectAll 这样的方法:

The best solution I've found for this issue so far is to create a custom UITextView (i.e. create a new class that extends UITextView) and then implement the selectAll method like this:

- (void)selectAll:(id)sender {
    [super selectAll:sender];
    UITextRange *selectionRange = [self textRangeFromPosition:self.beginningOfDocument toPosition:self.endOfDocument];
    [self performSelector:@selector(setSelectedTextRange:) withObject:selectionRange afterDelay:0.0];
}

然后当您使用文本视图时,将其类型设置为自定义文本视图类型(在您的代码和故事板中)。现在,您可以在需要时成功调用 selectAll 方法。我想这也适用于 UITextField ,但我还没有尝试过。

Then when you use a text view, set its type to your custom text view type (in your code and in the storyboard). Now you can successfully call the selectAll method whenever you need to. I suppose this should work with UITextField too, but I haven't tried it yet.

这篇关于UITextView selectAll方法无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆