iOS 7.1 UITextView在新行之后仍然没有滚动到光标/插入符号 [英] iOS 7.1 UITextView still not scrolling to cursor/caret after new line

查看:213
本文介绍了iOS 7.1 UITextView在新行之后仍然没有滚动到光标/插入符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从iOS 7开始,当用户键入流向新行的文本时, UITextView 不会自动滚动到光标。 SO和其他地方都有很好的文档记录。对我来说,这个问题仍然存在于iOS 7.1中。我做错了什么?

Since iOS 7, a UITextView does not scroll automatically to the cursor as the user types text that flows to a new line. This issue is well documented on SO and elsewhere. For me, the issue is still present in iOS 7.1. What am I doing wrong?

我安装了Xcode 5.1并针对iOS 7.1。我正在使用自动布局。

I installed Xcode 5.1 and targeted iOS 7.1. I'm using Auto Layout.

以下是我如何将文本视图的内容放在键盘上方:

Here's how I position the text view's content above the keyboard:

- (void)keyboardUp:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    UIEdgeInsets contentInset = self.textView.contentInset;
    contentInset.bottom = keyboardRect.size.height;
    self.textView.contentInset = contentInset;
}

我尝试了什么:我尝试过很多次在这个问题上发布到SO的解决方案,因为它与iOS 7有关。我尝试过的所有解决方案似乎对于显示属性字符串的文本视图效果很好。在以下三个步骤中,我概述了如何在SO上获得最多投票答案( https://stackoverflow.com/a/19277383/ 1239263 )响应用户第一次点击返回键。

What I have tried: I have tried many of the solutions posted to SO on this issue as it pertains to iOS 7. All of the solutions that I have tried do not seem to hold up well for text views displaying an attributed string. In the following three steps, I outline how the most up-voted answer on SO (https://stackoverflow.com/a/19277383/1239263) responds to the user tapping the return key for the first time.

(1。)文本视图成为 viewDidLoad 中的第一个响应者。滚动到光标所在文本视图的底部。

(1.) The text view became the first responder in viewDidLoad. Scroll to the bottom of the text view where the cursor is located.

(2。)在键入单个字符之前,请点击键盘上的返回键。插入符号消失在视线之外。

(2.) Before typing a single character, tap the return key on the keyboard. The caret disappears out of sight.

(3。)然而,再次点击返回键似乎可以使情况正常化。 (注意:删除后一个新行会使插入符号再次消失)。

(3.) Tapping the return key again, however, seems to normalize the situation. (Note: deleting the latter new line, however, makes the caret disappear once again).

推荐答案

改进了 UITextView的解决方案代码后代类:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define is_iOS7 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#define is_iOS8 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")

@implementation MyTextView {
    BOOL settingText;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextViewDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated {
    CGRect rect = [textView caretRectForPosition:textView.selectedTextRange.end];
    rect.size.height += textView.textContainerInset.bottom;
    [textView scrollRectToVisible:rect animated:animated];
}

- (void)handleTextViewDidChangeNotification:(NSNotification *)notification {
    if (notification.object == self && is_iOS7 && !is_iOS8 && !settingText) {
        UITextView *textView = self;
        if ([textView.text hasSuffix:@"\n"]) {
            [CATransaction setCompletionBlock:^{
                [self scrollToCaretInTextView:textView animated:NO];
            }];
        } else {
            [self scrollToCaretInTextView:textView animated:NO];
        }
    }
}

- (void)setText:(NSString *)text {
    settingText = YES;
    [super setText:text];
    settingText = NO;
}

注意在蓝牙键盘上按下向下键时它不起作用。

Note it doesn't work when Down key is pressed on Bluetooth keyboard.

这篇关于iOS 7.1 UITextView在新行之后仍然没有滚动到光标/插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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