UITextView keyboard

from:http://inessential.com/2014/01/07/uitextview_scroll-to-typing_bug

UITextView Scroll-to-Typing Bug

As I’ve said many times, I’m a huge fan of Text Kit. It’s a big deal for Vesper. Among other things, it lets us do bolding/un-bolding in a way that didn’t work reliably or efficiently in iOS 6.

But with Text Kit came a new UITextView. The old one was based on WebView, and this one isn’t. That makes it a 1.0 version of UITextView, since it’s a new thing. And it has some bugs. (Not surprisingly for a 1.0.)

I’ve been trying to figure out work-arounds for these. I’m making progress.

Set the frame, not the contentInset

People Who Know(tm) (Greg Pierce, for one) have advised me not to set the contentInset of the UITextView when the keyboard appears and disappears. Instead change the frame. In my testing this makes a big difference.

It means you don’t get the blurred-out UITextView underneath the keyboard, which is too bad, but that trade-off is easy to live with.

Scrolling to show typing

I have this almost working. Like a bottle of wine I just can’t manage to un-cork.

From Greg Pierce (again), author of Drafts, I have this code:

- (void)textViewDid​ChangeSelection:​(UITextView *)textView {
 
  [textView layoutIfNeeded];

  CGRect caretRect = [textView caretRectForPosition:​textView.selectedTextRange.end];
  NSLog(@"y %f", caretRect.origin.y);
  caretRect.size.height += textView.textContainerInset.bottom;
 
  [textView scrollRectToVisible:caretRect animated:NO];
}

You can download a super-simple sample project with this code.

The one place it fails:

  1. Tap at the end of the text.
  2. Tap Return.

The caret is hidden. Type any other key and it un-hides. But I really, really want the caret to be not hidden right then.

If you look in the console you’ll note that caretRect.origin.y is just plain incorrect when you tap Return.

You can tap Return again and then backspace and see the same thing — only caretRect.origin.y will be wrong in a different direction.

So close. I can almost taste the wine. If you have any ideas, please let me know. I’ve tried a bunch of things and I’m willing to try more.

07 Jan 2014
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值