ios怎么让textview遵循scrollview中edittext的代理方法

匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。内容字号:
段落设置:
字体设置:
iOS TextView自适应高度以及键盘遮挡问题
1. 制作一个textview,遵守协议,设置相关属性,这里是我做的例子
协议:UITextViewDelegate
textview.backgroundColor=[UIColor whiteColor]; //背景色
textview.scrollEnabled = NO;& & //当文字超过视图的边框时是否允许滑动,默认为&YES&
textview.delegate =& & & //设置代理方法的实现类
textview.font=[UIFont fontWithName:@&Arial& size:18.0]; //设置字体名字和字体大小;
textview.returnKeyType = UIReturnKeyD//return键的类型
//& & textview.keyboardType = UIKeyboardTypeD//键盘类型换行
textview.returnKeyType = UIReturnKeyD//键盘类型完成& 不是用完成的话 当文字过多 无法回收键盘
//& & textview.textAlignment = NSTextAlignmentL //文本显示的位置默认为居左
textview.dataDetectorTypes = UIDataDetectorTypeA //显示数据类型的连接模式(如电话号码、网址、地址等)
textview.textColor = [UIColor blackColor];
textview.autoresizingMask = UIViewAutoresizingFlexibleH
textview.text =NSLocalizedString(@&qingshuru&,@&&);//设置显示的文本内容
self.automaticallyAdjustsScrollViewInsets=NO;
2. 实现高度自适应
//内容改变自动调用 自适应文本高度
- (void)textViewDidChange:(UITextView *)textV
(2)在上面的方法中写入
CGSize size=[textview sizeThatFits:CGSizeMake(CGRectGetWidth(textview.frame), MAXFLOAT)];
CGRect frame=textview.
frame.size.height=size.
textview.frame=
这样就完成了自适应高度
(3)对文字输入的字数进行限制
if([[self currentLanguage] compare:@&zh-Hans-CN& options:NSCaseInsensitiveSearch]==NSOrderedSame || [[self currentLanguage] compare:@&zh-Hant-CN& options:NSCaseInsensitiveSearch]==NSOrderedSame)
if (textView.text.length & 300)
textView.text = [textView.text substringToIndex:300];
if (textView.text.length & 500)
textView.text = [textView.text substringToIndex:500];
3. 实现当文字过多键盘遮挡问题
(1)对textview设置监听
//注册通知,监听键盘出现
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(handleKeyboardDidHidden)
name:UIKeyboardDidHideNotification
object:nil];
(2)实现监听方法
- (void)handleKeyboardDidShow:(NSNotification*)paramNotification
NSLog(@&监听方法&);
//获取键盘高度
keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
[keyboardRectAsObject getValue:&keyboardRect];
textview.contentInset=UIEdgeInsetsMake(0, 0,keyboardRect.size.height, 0);
animationTime = [paramNotification.userInfo[@&UIKeyboardAnimationDurationUserInfoKey&] floatValue];
- (void)handleKeyboardDidHidden
textview.contentInset=UIEdgeInsetsZ
(3)实现textview的delegate方法
在- (void)textViewDidChange:(UITextView *)textV方法中写入
self.view.transform = CGAffineTransformI
UIView *editView =
CGRect tfRect = [editView.superview convertRect:editView.frame toView:self.view];
//& & & & NSLog(@&%@&, keyboardRectAsObject);
CGRect keyBoardF = [keyboardRectAsObject CGRectValue];
CGFloat _editMaxY = CGRectGetMaxY(tfRect);
CGFloat _keyBoardMinY = CGRectGetMinY(keyBoardF);
//& & & & NSLog(@&%f %f&, _editMaxY, _keyBoardMinY);
if (keyboardRectAsObject) {
if (_keyBoardMinY & _editMaxY && _editMaxY-_keyBoardMinY&20) {
CGFloat moveDistance = _editMaxY - _keyBoardMinY;
[UIView animateWithDuration:animationTime animations:^{
textview.transform = CGAffineTransformTranslate(textview.transform, 0, -moveDistance);
此时已经实现了高度自适应和键盘遮挡问题
分享给小伙伴们:
本类最热新闻
48小时最热
0102030405060708910
CopyRight © 2015- , All Rights Reserved.
清屏网 版权所有 豫ICP备号推荐这篇日记的豆列
······&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!

我要回帖

更多关于 textview scrollbar 的文章

 

随机推荐