相當(dāng)于常見的反饋意見功能,要求有如下4點:
1.輸入文字的時候提示文字消失法梯,TextView沒有文字的時候提示文字顯示;
2.右下角實時顯示字?jǐn)?shù)犀概;
3.字?jǐn)?shù)到達(dá)指定限制后立哑,TextView不能輸入更多,可以刪除姻灶;
4.提交按鈕在TextView不為空的時候按鈕為綠色且可點擊铛绰;TextView為空時,為灰色狀態(tài)且不可點擊产喉。
效果:
代碼實現(xiàn):
// ViewController.m
// DHTextViewSetting
//
// Created by 鄧昊 on 2017/12/7.
// Copyright ? 2017年 鄧昊. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<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 ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.feedBackTextView.delegate = self;
self.placeHolder.userInteractionEnabled = NO;
self.commitButton.userInteractionEnabled = NO;
self.placeHolder.text = @"請輸入您的意見...";
self.feedBackTextView.layer.borderWidth = 0.5;
self.feedBackTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
#pragma mark - UITextViewDelegate
//正在改變
- (void)textViewDidChange:(UITextView *)textView {
NSLog(@"%@", textView.text);
self.placeHolder.hidden = YES;
//允許提交按鈕點擊操作
self.commitButton.backgroundColor = [UIColor blueColor];
self.commitButton.userInteractionEnabled = YES;
//實時顯示字?jǐn)?shù)
self.stirngLenghLabel.text = [NSString stringWithFormat:@"%lu/20", (unsigned long)textView.text.length];
//字?jǐn)?shù)限制操作
if (textView.text.length >= 20) {
textView.text = [textView.text substringToIndex:20];
self.stirngLenghLabel.text = @"20/20";
}
//取消按鈕點擊權(quán)限捂掰,并顯示提示文字
if (textView.text.length == 0) {
self.placeHolder.hidden = NO;
self.commitButton.userInteractionEnabled = NO;
self.commitButton.backgroundColor = [UIColor lightGrayColor];
}
}
@end