UITextView cursor not positioning properly when editing in iOS 7. Why?(在 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 光标无法正确定位.为什
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
