您的位置:首页 > 移动开发 > IOS开发

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 字数限制
相关文章推荐