[ios]UITextView 如何保持键盘上方光标

发布时间: 2016/5/9 4:17:21
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 ViewController 与 UITextView 占整体来看,与顶部的导航栏。几乎就像苹果公司的"注意到"应用程序。想实现什么是保持 textview 的光标可见,当编辑的开始,或编辑。

得以在游标的 CGPoint ,但我有困难的时候,计算点滚动。我如何可以做到这一点?

谢谢你

当 textview 开始编辑

- (void)keyboardDidShow:(NSNotification*)aNotification {

    // Keyboard
    NSDictionary *info = [aNotification userInfo];
    CGRect keyPadFrame = [[UIApplication sharedApplication].keyWindow convertRect:[[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:self.view];
    CGSize keyboardSize = keyPadFrame.size;
    kbSize = keyboardSize;

    [self scrollToCursor];
}

当 textview 编辑

- (void)textViewDidChange:(UITextView *)textView {
    // Scroll to cursor
    [self scrollToCursor];
}

滚动到光标的方法

- (void)scrollToCursor {
    // View
    CGRect viewBounds = self.view.bounds;
    CGRect visibleViewBounds = CGRectMake(viewBounds.origin.x,
                                          viewBounds.origin.y + (self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height),
                                          viewBounds.size.width,
                                          viewBounds.size.height - (kbSize.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height));

    // TextView
    CGPoint textViewOrigin = [self.view convertRect:self.noteTextView.frame fromView:self.noteTextView.superview].origin;

    // Cursor
    CGPoint textViewCursor = [self.noteTextView caretRectForPosition:self.noteTextView.selectedTextRange.start].origin;
    CGPoint cursorPoint = CGPointMake((textViewCursor.x + textViewOrigin.x), (textViewCursor.y - self.noteTextView.contentOffset.y));

    // Scroll to point
    if (!CGRectContainsPoint(visibleViewBounds, CGPointMake(cursorPoint.x, cursorPoint.y + 25/*25 for cursor's height*/))) {
        [self.noteTextView setContentOffset:CGPointMake(0, 0)/*How to calculate??*/ animated:YES];
    }
}

解决方法 1:

这根本没有测试到,但会在它我第一次尝试。

通过 KeyboardWillChangeFrameNotification 听得到键盘高度

CGRect keyboardFrame;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)keyboardWillChange:(NSNotification *)notification {
    keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrame = [self.view convertRect:keyboardRect fromView:nil];
}

这将给你的键盘高度。

然后获取屏幕高度 ︰

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;  
CGFloat screenHeight = screenSize.height;

然后如果你知道光标的起始,做这样的事情 ︰

CGFloat keyboardTop = (screenHeight - (keyboardFrame.size.height + <padding if you want it>));
if (currentCursorPosition.y > keyboardTop)
{
 [self.noteTextView setContentOffset:CGPointMake(0, (cursorPoint.y - (viewBounds.size.height - kbSize.height)) + self.noteTextView.contentOffset.y + 25);
}

理想的情况是这应,将光标保留在键盘顶部,然后滚动当你向下移动光标。

官方微信
官方QQ群
31647020