码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-27 11:27:17      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

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

有时候我们使用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 }

具体效果如下:

技术分享

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

标签:

原文地址:http://www.cnblogs.com/student-hualei/p/4603733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!