ios开发之--实现TextView提示文字并且输入字数限制
Posted 稻草人11223
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios开发之--实现TextView提示文字并且输入字数限制相关的知识,希望对你有一定的参考价值。
实现字数控制和限制,并展示当前输入的字符串长度,以及按钮的点击状态,代码如下:
1,设置代理
@interface FKViewController ()<UITextViewDelegate> @property (weak, nonatomic) IBOutlet UITextView *FKTextView; @property (weak, nonatomic) IBOutlet UILabel *descLab; @property (weak, nonatomic) IBOutlet UIButton *sendBtn; @property (weak, nonatomic) IBOutlet UILabel *stringlenghtLab;
2,设置初始状态
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.view.backgroundColor = BackgroundColor; self.navigationController.navigationBarHidden = YES; CustomNavigationBar *nav = [[CustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, KscreenW, NavHeight) withTitle:@"意见反馈" withLeftBtnHidden:NO withRightBtn:YES]; nav.delegate = self; [self.view addSubview:nav]; self.FKTextView.delegate = self; self.descLab.userInteractionEnabled = NO; self.sendBtn.userInteractionEnabled = NO; self.sendBtn.backgroundColor = [UIColor lightGrayColor]; }
3,代理方法的实现
-(void)textViewDidChange:(UITextView *)textView { self.descLab.hidden = YES; self.sendBtn.userInteractionEnabled = YES; self.sendBtn.backgroundColor = MainColor; //实时显示字数 self.stringlenghtLab.text = [NSString stringWithFormat:@"%ld/100",(long)textView.text.length]; //字数限制 if (textView.text.length >= 100) { textView.text = [textView.text substringToIndex:100]; } //取消安润点击权限,并显示文字 if (textView.text.length == 0) { self.descLab.hidden = NO; self.sendBtn.userInteractionEnabled = NO; self.sendBtn.backgroundColor = [UIColor lightGrayColor]; } }
字符串长度为0时,按钮返回初始状态,有输入字符时,显示为可点击状态,很简单,仅做记录用!
以上是关于ios开发之--实现TextView提示文字并且输入字数限制的主要内容,如果未能解决你的问题,请参考以下文章
TextView能否实现图片在左边,文字在右边,并且水平居中?