UITextView自适应内容的高度且实现光标总是距离键盘一段距离而滑动条却可以延伸到光盘

这几天遇到个想要实现的效果,如题,想了一段时间,昨天实现了,这里说下我实现的方法。

有时候我们使用uitextview编辑文本时,在输入框贴着键盘的情况下(即不设置UITextView的frame距离键盘一段距离)想让输入时光标一直距离键盘一段距离,这样用户的输入体验会好很多,UItextView没有发现这样的代理方法或属性来直接实现这一效果,于是我考虑用一个UIScrollVIew和一个UITextView来实现,即即将UITextVIew贴在UIScrollView上,然后通过UITextView的代理方法-textViewDidChange:,来不断让UITextView的height自适应输入的内容高度,同时通过动画执行改变UIScrollView的frame和contentOffset,以适应UITextView的高度改变以及输入的内容超过所给输入空间时输入框向上不断随输入内容而滑动。

具体代码如:

1.在viewDidLoad里面创建一个UIScrollVIew和一个UITextView:

 1     self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
 2     self.mainScrollView.backgroundColor = [UIColor whiteColor];
 3     self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
 4     self.mainScrollView.scrollsToTop = YES;
 5     self.mainScrollView.delegate = self;
 6
 7     self.debrisTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, SCREEN_HEIGHT)];
 8     self.debrisTextView.font = [UIFont fontWithName:@"Arial" size:20];
 9     self.debrisTextView.delegate = self;
10     self.debrisTextView.backgroundColor = [UIColor whiteColor];

2.获取键盘的高度并据此改变UIScrollView的frame:

 1 //获取键盘的高度并根据键盘的高度进行重新布局。
 2 - (void)notificationCenterForKeyboard
 3 {
 4     //增加监听,当键盘出现或消失时发出消息。
 5     [[NSNotificationCenter defaultCenter] addObserver:self
 6                                              selector:@selector(keyboardWillShow:)
 7                                                  name:UIKeyboardWillShowNotification
 8                                                object:nil];
 9
10     [[NSNotificationCenter defaultCenter] addObserver:self
11                                              selector:@selector(keyBoardWillHide:)
12                                                  name:UIKeyboardWillHideNotification
13                                                object:nil];
14 }
15 //当键盘出现或改变时调用
16 - (void)keyboardWillShow:(NSNotification *)aNotification
17 {
18     //获取键盘的高度
19     NSDictionary *userInfo = [aNotification userInfo];
20     NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
21     CGRect keyboardRect = [aValue CGRectValue];
22     CGFloat height = keyboardRect.size.height;
23
24     self.mainScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - height - 70);
25 }
26 //当键盘退出时调用
27 - (void)keyBoardWillHide:(NSNotification *)aNotification
28 {
29     self.mainScrollView.frame = CGRectMake(10, 0, SCREEN_WIDTH - 20, SCREEN_HEIGHT);
30 }

3.通过TextViewDidChange:方法实现UITextView和UIScrollVIew的frame及contentOffset不断调整适应输入内容:

 1 //UITextView的代理方法。
 2 -(void)textViewDidChange:(UITextView *)textView
 3 {
 4     [textView flashScrollIndicators];
 5     static CGFloat maxHeight = 280.0f;
 6
 7     CGRect frame = textView.frame;
 8     CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
 9     CGSize size = [textView sizeThatFits:constraintSize];
10     textView.scrollEnabled = NO;
11
12     textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
13
14     if (size.height > maxHeight) {
15         [UIView animateWithDuration:0.2 animations:^{
16             self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, size.height + 150);
17             self.mainScrollView.contentOffset = CGPointMake(0, size.height - maxHeight);
18         }];
19     }
20 }

具体效果如下:

时间: 2024-08-28 18:25:48

UITextView自适应内容的高度且实现光标总是距离键盘一段距离而滑动条却可以延伸到光盘的相关文章

获取textarea文本框所选字符光标位置索引,以及选中的文本值;textarea高度自适应,随着内容增加高度增加;获取输入框中的光标位置

获取textarea文本框所选字符光标位置索引,以及选中的文本值 $.fn.selection = function () { var s, e, range, stored_range; if (this[0].selectionStart == undefined) { var selection = document.selection; if (this[0].tagName.toLowerCase() != "textarea") { var val = this.val()

iOS 8:【转】iOS UITextView 输入内容实时更新 cell 的高度

源地址:http://vit0.com/blog/2014/12/25/ios-textview-in-cell/ 这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度.自动计算 cell 高度的功能使用 iOS 8 才支持的自适应 cell,如果你还不知道 iOS 8 自适应 cell,可以参看这篇文章:iOS 8 自适应 Cell 先上图,我们最终要实现的效果是这样的: 图 1:实时更新

iOS UITextView 输入内容实时更新cell的高度

先上图,我们最终要实现的效果是这样的:可参考(http://www.cocoachina.com/ios/20141226/10778.html) 图 1:实时更新 cell 高度 实现上面效果的基本原理是: 在 cell 中设置好 text view 的 autolayout,让 cell 可以根据内容自适应大小 text view 中输入内容,根据内容更新 textView 的高度 调用 tableView 的 beginUpdates 和 endUpdates,重新计算 cell 的高度

iOS UITextView自适应高度UITextContainerView抖动问题

在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scrollRangeToVisible 方法解决了我的问题(Swift 3): textView.scrollRangeToVisible(NSRange.init(location: 0, length: 0)) 获取UITextView内的文字高度以及行数的方法(Swift 3): let heig

UITableViewCell自适应内容高度

UITableViewCell自适应内容高度 (2015-06-25 16:02:31) 其实TableViewCell自适应高度也就是Cell中的label自适应高度,网上有好多label自适应高度的方法,可以百度. 除了上面说的,还有一种比较简单的,其实原理估计都是一样的,只是个人觉得这种更好些而已. 首先我们知道返回cell自适应高度,那么这个高度怎么来,肯定是TableViewDelegate的方法- (CGFloat)tableView:(UITableView *)tableView

【转】UILabel、UITextView自适应得到高度

原文:http://blog.csdn.net/xcysuccess3/article/details/8331549 在iOS中,经常遇到需要根据字符串的内容动态指定UILabel,UITextView,UITableViewCell等的高度的情况,这个时候就需要动态的计算字符串内容的高度,下面是计算的方法: [cpp] view plaincopy /** @method 获取指定宽度情况ixa,字符串value的高度 @param value 待计算的字符串 @param fontSize

jquery库实现iframe自适应内容高度和宽度

javascript原生和jquery库实现iframe自适应内容高度和宽度---推荐使用jQuery的代码! ‍<iframe src="index.php" id="mainiframe" name="mainiframe" width="100%"   frameborder="0" scrolling="no" marginwidth="0" margi

swift webView的高度自适应内容

废话不多 直接上代码 //在webView的协议方法里实现以下代码 func webViewDidFinishLoad(webView: UIWebView) {//加载完成 //        self.view.addSubview(webView) webView.scrollView.mj_header.endRefreshing() self.mainView.removeFromSuperview() //计算webView内容的高度 然后去改变webView的高度 嗯呢 就是这样

完美方案——iOS的WebView自适应内容高度

/////////////////////////////初始化,self.view是父控件///////////////////////////////// _webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 0)]; _webView.delegate = self; _webView.scrollView.bounces = NO; _webView.scroll