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

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


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

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

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

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

.m中的代码如下:

#import "YJFeedBackViewController.h"


@interface YJFeedBackViewController ()<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 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 = [UIColor lightGrayColor].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 = [NSString stringWithFormat:@"%lu/100", (unsigned long)textView.text.length];

    

    //字数限制操作

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

        

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

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

        

    }

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

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

        

        self.placeHolder.hidden = NO;

        self.commitButton.userInteractionEnabled = NO;

        self.commitButton.backgroundColor = [UIColor lightGrayColor];


    }


}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sailip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值