iOS实现TextView提示文字并且输入字数限制
实现TextView提示文字并且输入字数限制的反馈意见功能—–非常简洁明了!如图实现页面,效果如下:
1.输入文字的时候提示文字消失,TextView没有文字的时候提示文字显示;
2.右下角实时显示字数;
3.字数到达指定限制后,TextView不能输入更多,可以删除;
4.提交按钮在TextView不为空的时候按钮为绿色且可点击;TextView为空时,为灰色状态且不可点击。
.m中的代码如下:
#import “YJFeedBackViewController.h”
@interface YJFeedBackViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *placeHolder;
@property (weak, nonatomic) IBOutlet UIButton *commitButton;
@property (weak, nonatomic) IBOutlet UITextView *feedBackTextView;
@property (weak, nonatomic) IBOutlet UILabel *stirngLenghLabel;
@end
@implementation YJFeedBackViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.title = @”反馈意见”;
self.automaticallyAdjustsScrollViewInsets = NO;
self.feedBackTextView.delegate = self;
self.placeHolder.userInteractionEnabled = NO;
self.commitButton.userInteractionEnabled = NO;
self.feedBackTextView.layer.borderWidth = 0.5;
self.feedBackTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
// Do any additional setup after loading the view from its nib.
}
//正在改变
– (void)textViewDidChange:(UITextView *)textView
{
FDLog(@”%@”, textView.text);
self.placeHolder.hidden = YES;
//允许提交按钮点击操作
self.commitButton.backgroundColor = FDMainColor;
self.commitButton.userInteractionEnabled = YES;
//实时显示字数
self.stirngLenghLabel.text = [NSString stringWithFormat:@”%lu/100″, (unsigned long)textView.text.length];
//字数限制操作
if (textView.text.length >= 100) {
textView.text = [textView.text substringToIndex:100];
self.stirngLenghLabel.text = @”100/100″;
}
//取消按钮点击权限,并显示提示文字
if (textView.text.length == 0) {
self.placeHolder.hidden = NO;
self.commitButton.userInteractionEnabled = NO;
self.commitButton.backgroundColor = [UIColor lightGrayColor];
}
}