UITextView 评论输入框 高度自适应
来源:互联网 发布:中国8月金融数据 编辑:程序博客网 时间:2024/06/05 18:40
创建一个inputView继承于UIView
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorcolorWithRed:0.13green:0.13blue:0.13alpha:0.30];
//使用NSNotificationCenter鍵盤出現時
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotificationobject:nil];
//使用NSNotificationCenter鍵盤隐藏時
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotificationobject:nil];
[selfcreateView];
// 点击黑色view 回收键盘
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapAction)];
tap.delegate =self;
self.userInteractionEnabled =YES;
[selfaddGestureRecognizer:tap];
}
returnself;
}
- (void)createView{
// 输入框view
self.inputView = [[UIViewalloc]initWithFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width,50)];
[selfaddSubview:self.inputView];
self.inputView.backgroundColor = [UIColorcolorWithRed:0.80green:0.82blue:0.84alpha:1.00];
// 输入框
self.inputTextView = [[UITextViewalloc]initWithFrame:CGRectMake(10,10, [UIScreenmainScreen].bounds.size.width-20,30)];
self.inputTextView.delegate = self;
self.inputTextView.returnKeyType = UIReturnKeySend;
self.inputTextView.layer.cornerRadius = 4;
self.inputTextView.layer.masksToBounds = YES;
self.inputTextView.font = [UIFont systemFontOfSize:12];
self.inputTextView.backgroundColor = [UIColorcolorWithRed:0.95green:0.95blue:0.95alpha:1.00];
self.inputTextView.textColor = [UIColorblackColor];
[self.inputViewaddSubview:self.inputTextView];
// 占位文字
self.placeLabel = [[UILabelalloc]initWithFrame:CGRectMake(6,10,200, 10)];
self.placeLabel.text = @"最多可输入300字";
self.placeLabel.textColor = [UIColorlightGrayColor];
[self.inputTextViewaddSubview:self.placeLabel];
//self.placeLabel.backgroundColor = [UIColor yellowColor];
self.placeLabel.font = [UIFont systemFontOfSize:12];
self.placeLabel.userInteractionEnabled = NO;
self.placeLabel.hidden =NO;
}
//这个函数的最后一个参数text代表你每次输入的的那个字,所以:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
// NSLog(@"-----%@",text);
if ([textisEqualToString:@""]) {
//表示删除字符
}
// 如果点击的是发送按钮,,进行发送操作
if ([textisEqualToString:@"\n"]){
if (self.inputTextView.text.length>0) {
[self.delegatesendText:self.inputTextView.text];
}
returnNO;
}else{
// 正常输入,计算输入字符长度限制在300字
NSString *new = [textView.textstringByReplacingCharactersInRange:rangewithString:text];
if(new.length >300){
if (![textisEqualToString:@""]) {
returnNO;
}
}
returnYES;
}
}
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length ==0) {
// 只要输入的内容是空 就显示占位字符
self.placeLabel.hidden =NO;
}else{
self.placeLabel.hidden =YES;
}
// 规定输入框最大高度。
staticCGFloat maxHeight =120.0f;
CGRect frame = textView.frame;
// 计算文字高度
CGSize constraintSize =CGSizeMake(frame.size.width,MAXFLOAT);
CGSize size = [textViewsizeThatFits:constraintSize];
if (size.height >= maxHeight){
size.height = maxHeight;
textView.scrollEnabled =YES; // 大于最大高度允许滚动
}else{
textView.scrollEnabled =NO; // 小于最大高度不允许滚动
}
[UIViewanimateWithDuration:0.2animations:^{
// 动态的改变输入View的尺寸
self.inputView.frame =CGRectMake(0, [UIScreenmainScreen].bounds.size.height-self.keyboldH-size.height-20, [UIScreen mainScreen].bounds.size.width, size.height+20);
// 动态的改变输入框的尺寸
textView.frame =CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
}];
}
//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWillShown:(NSNotification*)aNotification{
NSDictionary* info = [aNotificationuserInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;//得到鍵盤的高度
self.keyboldH = kbSize.height;
CGFloat height =0;
// 切换键盘的时候重新计算文字高度 设置输入view和输入框尺寸
if (self.inputTextView.text == nil) {
height = 50;
}else{
CGSize constraintSize =CGSizeMake([UIScreenmainScreen].bounds.size.width-20,MAXFLOAT);
CGSize size = [self.inputTextViewsizeThatFits:constraintSize];
height = size.height;
}
[UIViewanimateWithDuration:0.1animations:^{
if (height>=120.0f) {
self.inputView.frame =CGRectMake(0, [UIScreenmainScreen].bounds.size.height - kbSize.height-120.0f-20, [UIScreenmainScreen].bounds.size.width,120.0f+20);
}else{
self.inputView.frame =CGRectMake(0, [UIScreenmainScreen].bounds.size.height - kbSize.height-height-20, [UIScreenmainScreen].bounds.size.width, height+20);
}
}];
}
//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
[UIViewanimateWithDuration:1animations:^{
self.inputView.frame = CGRectMake(0, [UIScreenmainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width,50);
}];
}
- (void)inputViewShow{
[self.inputTextViewbecomeFirstResponder];
}
- (void)inputViewHiden{
[self.inputTextViewresignFirstResponder];
}
- (void)tapAction{
[selfremoveFromSuperview];
[selfinputViewHiden];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if (touch.view ==self.inputView ) {
returnNO;
}else{
returnYES;
}
}
- UITextView 评论输入框 高度自适应
- 模拟新浪微博评论textarea输入框随着输入内容增多输入框高度自适应
- UITextView自适应高度,同时可以设置最大高度, 类似于QQ微信输入框效果,支持表情的输入
- 输入框高度自适应
- UILabel, UITextView 高度自适应
- IOS UITextView自适应高度
- IOS UITextView自适应高度
- iOS:UITextView自适应高度
- UILabel, UITextView 高度自适应
- IOS UITextView自适应高度
- UITextView自适应高度
- IOS UITextView自适应高度
- UITextView自适应高度
- IOS UITextView自适应高度
- iOS UITextView输入时自适应高度,placeholderLabel的设置。
- UITextView输入框,动态算高度
- 文本输入框自适应高度
- 高度自适应的输入框
- Lodop打印参数方法参考
- myeclipse获取注册码
- 给拒绝过你的公司再投简历,是自取其辱?
- Oracle创建新用户一系列操作模板
- 一个便于优化的网站应该具有哪些细节
- UITextView 评论输入框 高度自适应
- Codeforces Round #409 Div. 2(A+B)
- 并发工具类(三)控制并发线程数的Semaphore
- 道理我都懂,可我就是不爱讲道理啊
- 浮点数判等
- TP5分页传参的形式改变
- 使用BigDecimal进行精确运算
- webuploader 跨域问题
- opencv学习(四十四)之图像角点检测Harris