iOS - 实现TextView提示文字并且输入字数限制

#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) forControlEvents: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值