在 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 光标无法正确定位.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)