iOS - 实现TextView提示文字并且输入字数限制
2017-03-02 22:33
567 查看
#import "FeedBackViewController.h" #import <Masonry.h> #define KCColor(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]//颜色宏定义 #define KCGrayColor KCColor(204,204,204) #define KCGreenColor KCColor(32,153,133) #define KCWhiteColor KCColor(250,249,245) @interface FeedBackViewController () <UITextViewDelegate, UINavigationControllerDelegate> @property (nonatomic, strong) UITextView *feedBackTextView; @property (nonatomic, strong) UIButton *commitButton; @property (nonatomic, strong) UILabel *placeHolderLabel; @property (nonatomic, strong) UILabel *stringLengthLabel; @end @implementation FeedBackViewController #pragma mark ------------------------------life circle----------------------------------- - (void)viewDidLoad { [super viewDidLoad]; [self initFeedBackText]; [self initButton]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark -----------------------------private method--------------------------------- -(void) initFeedBackText{ _feedBackTextView = [[UITextView alloc] init]; self.feedBackTextView.delegate = self; _feedBackTextView.backgroundColor = [UIColor whiteColor]; _feedBackTextView.font = [UIFont systemFontOfSize:14]; self.automaticallyAdjustsScrollViewInsets = NO; [self.view addSubview:self.feedBackTextView]; [self.feedBackTextView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width, 150)); make.top.equalTo(self.view.mas_top).offset(64); make.centerX.equalTo(self.view.mas_centerX); }]; _placeHolderLabel = [[UILabel alloc] init]; _placeHolderLabel.text = @"欢迎留下您的反馈"; _placeHolderLabel.textColor = KCGrayColor; _placeHolderLabel.font = [UIFont systemFontOfSize:14]; self.placeHolderLabel.userInteractionEnabled = NO; [self.view addSubview:self.placeHolderLabel]; [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.mas_left).offset(16); make.top.equalTo(self.feedBackTextView.mas_top).offset(16); }]; _stringLengthLabel = [[UILabel alloc] init]; _stringLengthLabel.textColor = KCGrayColor; _stringLengthLabel.text = @"100"; _stringLengthLabel.font = [UIFont systemFontOfSize:12]; [self.view addSubview:self.stringLengthLabel]; [self.stringLengthLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view.mas_right).offset(-16); make.bottom.equalTo(self.feedBackTextView.mas_bottom).offset(-10); }]; } -(void) initButton{ _commitButton = [[UIButton alloc] init]; [_commitButton setTitle:@"提交" forState:UIControlStateNormal]; [_commitButton setTitleColor:KCWhiteColor forState:UIControlStateNormal]; _commitButton.titleLabel.font = [UIFont systemFontOfSize:17]; _commitButton.backgroundColor = KCGrayColor; _commitButton.layer.cornerRadius = 7; self.commitButton.userInteractionEnabled = NO; [self.commitButton addTarget:self action:@selector(commitButtonClicked) forControlEvent 4000 s:UIControlEventTouchUpInside]; [self.view addSubview:self.commitButton]; [self.commitButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(320, 40)); make.top.equalTo(self.feedBackTextView.mas_bottom).offset(174); make.centerX.equalTo(self.view.mas_centerX); }]; } #pragma mark -----------------------------textView delegate---------------------------------- -(void) textViewDidChange:(UITextView *)textView{ self.placeHolderLabel.hidden = YES; self.commitButton.backgroundColor = KCGreenColor; self.commitButton.userInteractionEnabled = YES; self.stringLengthLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)(100 - textView.text.length)]; if (textView.text.length >= 100) { textView.text = [textView.text substringToIndex:100]; self.stringLengthLabel.text = @"0"; } if (textView.text.length == 0) { self.placeHolderLabel.hidden = NO; self.commitButton.userInteractionEnabled = NO; self.commitButton.backgroundColor = KCGrayColor; } } @end
参考:http://blog.csdn.net/yj_sail/article/details/52254278
相关文章推荐
- ios开发之--实现TextView提示文字并且输入字数限制
- iOS实现TextView提示文字并且输入字数限制
- iOS TextView限制输入字数且屏蔽Emoji表情符号
- textfield/textview的子类,实现功能:几种不同类型的最大输入字符串字数限制,剩余可输入字符数,emoji表情过滤
- 【IOS】TextView里限制输入字数的方法
- iOS TextView限制输入字数且屏蔽Emoji表情符号
- iOS【textView或者textField文本输入字数限制】
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
- editText提示输入文字字数,同时限制字数和清空输入内容
- EditText 设置字数限制,超过字数限制时 toast 提示并且输入无效
- iOS 怎么限制textView和textField的输入字数
- 我的Android进阶之旅------>Android之AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)
- inupt textarea提示文字(点击消失,不输入恢复)及限制字数
- Android实现限制EditText输入文字的数量
- android AutoCompleteTextView 实现输入提示,类似百度支持输入拼音提示中文(gray)
- EditText限制输入字数,并且禁止输入表情符号
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)
- TextView里限制输入字数的方法