實(shí)現(xiàn)TextView提示文字并且輸入字?jǐn)?shù)限制的反饋意見功能-----非常簡(jiǎn)潔明了!如圖實(shí)現(xiàn)頁面,效果如下:
1.輸入文字的時(shí)候提示文字消失,TextView沒有文字的時(shí)候提示文字顯示;
2.右下角實(shí)時(shí)顯示字?jǐn)?shù);
3.字?jǐn)?shù)到達(dá)指定限制后,TextView不能輸入更多,可以刪除;
4.提交按鈕在TextView不為空的時(shí)候按鈕為綠色且可點(diǎn)擊;TextView為空時(shí),為灰色狀態(tài)且不可點(diǎn)擊。
.m中的代碼如下:
#import"YJFeedBackViewController.h"
@interfaceYJFeedBackViewController()
@property(weak,nonatomic)IBOutletUILabel*placeHolder;
@property(weak,nonatomic)IBOutletUIButton*commitButton;
@property(weak,nonatomic)IBOutletUITextView*feedBackTextView;
@property(weak,nonatomic)IBOutletUILabel*stirngLenghLabel;
@end
@implementationYJFeedBackViewController
- (void)viewDidLoad {
[superviewDidLoad];
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= [UIColorlightGrayColor].CGColor;
// Do any additional setup after loading the view from its nib.
}
//正在改變
- (void)textViewDidChange:(UITextView*)textView
{
FDLog(@"%@", textView.text);
self.placeHolder.hidden=YES;
//允許提交按鈕點(diǎn)擊操作
self.commitButton.backgroundColor=FDMainColor;
self.commitButton.userInteractionEnabled=YES;
//實(shí)時(shí)顯示字?jǐn)?shù)
self.stirngLenghLabel.text= [NSStringstringWithFormat:@"%lu/100", (unsignedlong)textView.text.length];
//字?jǐn)?shù)限制操作
if(textView.text.length>=100) {
textView.text= [textView.textsubstringToIndex:100];
self.stirngLenghLabel.text=@"100/100";
}
//取消按鈕點(diǎn)擊權(quán)限,并顯示提示文字
if(textView.text.length==0) {
self.placeHolder.hidden=NO;
self.commitButton.userInteractionEnabled=NO;
self.commitButton.backgroundColor= [UIColorlightGrayColor];
}
}