相当于常见的反馈意见功能,要求有如下4点:
1.输入文字的时候提示文字消失,TextView没有文字的时候提示文字显示;
2.右下角实时显示字数;
3.字数到达指定限制后,TextView不能输入更多,可以删除;
4.提交按钮在TextView不为空的时候按钮为绿色且可点击;TextView为空时,为灰色状态且不可点击。
效果:
textViewSetting
代码实现:
// 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;
//实时显示字数
self.stirngLenghLabel.text = [NSString stringWithFormat:@"%lu/20", (unsigned long)textView.text.length];
//字数限制操作
if (textView.text.length >= 20) {
textView.text = [textView.text substringToIndex:20];
self.stirngLenghLabel.text = @"20/20";
}
//取消按钮点击权限,并显示提示文字
if (textView.text.length == 0) {
self.placeHolder.hidden = NO;
self.commitButton.userInteractionEnabled = NO;
self.commitButton.backgroundColor = [UIColor lightGrayColor];
}
}
@end
网友评论