在 iOS 7 中编辑时 UITextView 光标无法正确定位.为什

UITextView cursor not positioning properly when editing in iOS 7. Why?(在 iOS 7 中编辑时 UITextView 光标无法正确定位.为什么?)

本文介绍了在 iOS 7 中编辑时 UITextView 光标无法正确定位.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面那个黄色框是一个可编辑的 UITextView,当前正在编辑(是第一响应者).但你可能分不清吧?因为没有游标.但是有……就是黄色textView左下角的那个小蓝点.它略低于 textViews 底部边框.因此,如果我继续换行或按回车键,文本将自然向上移动.但是光标永远不会是水平"的,或者是在 UITextView 的底部边框上方.它总是勉强从底部伸出,在边界下方几个点.

That yellow box above is an editable UITextView that is currently being edited (is first responder). But you probably couldn't tell right? Because there is no cursor. But there is... It's that small blue dot in the bottom left of the yellow textView. It's slightly below the textViews bottom border. So if I keep going to a new line, or pressing enter, the text will move up as it naturally should. But the cursor is never "level", or right above the UITextView's bottom border. It's always just barely poking out of the bottom, a couple points below the border.

为什么?这在 iOS 6 中不是问题.有什么方法可以解决这个问题?

Why? This wasn't a problem in iOS 6. Any way to fix this?

推荐答案

这个bug在iOS 7.0,你可以通过修改textView的委托方法来解决.

this bug is in iOS 7.0 you can solve this by modifying textView delegate method.

试试下面的代码

- (void)textViewDidChange:(UITextView *)textView {
    CGRect line = [textView caretRectForPosition:
        textView.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height
        - ( textView.contentOffset.y + textView.bounds.size.height
        - textView.contentInset.bottom - textView.contentInset.top );
    if ( overflow > 0 ) {
    // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
    // Scroll caret to visible area
        CGPoint offset = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
    // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
}

你的问题会解决的.

这篇关于在 iOS 7 中编辑时 UITextView 光标无法正确定位.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iOS 7 中编辑时 UITextView 光标无法正确定位.为什

基础教程推荐