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

来源:互联网 发布:python谷歌浏览器驱动 编辑:程序博客网 时间:2024/05/18 01:39

实现TextView提示文字并且输入字数限制的反馈意见功能-----非常简洁明了!如图实现页面,效果如下:


1.输入文字的时候提示文字消失,TextView没有文字的时候提示文字显示;

2.右下角实时显示字数;

3.字数到达指定限制后,TextView不能输入更多,可以删除;

4.提交按钮在TextView不为空的时候按钮为绿色且可点击;TextView为空时,为灰色状态且不可点击。

.m中的代码如下:

#import "YJFeedBackViewController.h"


@interface YJFeedBackViewController ()<UITextViewDelegate>


@property (weak,nonatomic) IBOutletUILabel *placeHolder;

@property (weak,nonatomic) IBOutletUIButton *commitButton;

@property (weak,nonatomic) IBOutletUITextView *feedBackTextView;

@property (weak,nonatomic) IBOutletUILabel *stirngLenghLabel;

@end


@implementation YJFeedBackViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    self.title =@"反馈意见";

    self.automaticallyAdjustsScrollViewInsets =NO;

    

    self.feedBackTextView.delegate =self;

    self.placeHolder.userInteractionEnabled =NO;

    self.commitButton.userInteractionEnabled =NO;

    

    self.feedBackTextView.layer.borderWidth =0.5;

    self.feedBackTextView.layer.borderColor = [UIColorlightGrayColor].CGColor;

    // Do any additional setup after loading the view from its nib.

}


//正在改变

- (void)textViewDidChange:(UITextView *)textView

{

    FDLog(@"%@", textView.text);

    

    self.placeHolder.hidden =YES;

    //允许提交按钮点击操作

    self.commitButton.backgroundColor =FDMainColor;

    self.commitButton.userInteractionEnabled =YES;

    //实时显示字数

    self.stirngLenghLabel.text = [NSStringstringWithFormat:@"%lu/100", (unsignedlong)textView.text.length];

    

    //字数限制操作

    if (textView.text.length >=100) {

        

        textView.text = [textView.textsubstringToIndex:100];

        self.stirngLenghLabel.text =@"100/100";

        

    }

    //取消按钮点击权限,并显示提示文字

    if (textView.text.length ==0) {

        

        self.placeHolder.hidden =NO;

        self.commitButton.userInteractionEnabled =NO;

        self.commitButton.backgroundColor = [UIColorlightGrayColor];


    }


}


0 0
原创粉丝点击