关于textView实时监控输入文字长度

之前在做实时监控textView的文字长度,控制输入文字的多少这个效果时,找不到方法,最后查看资料,找到了解决办法,分享给大家一下。

//设置输入框并且标明文字
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(effect.frame) + 10, SCREENW, 150)];
    self.inputView = inputView;
    inputView.backgroundColor = [UIColor whiteColor];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, SCREENW - 10, inputView.height - 30)];
    textView.backgroundColor = [UIColor clearColor];
    [inputView addSubview:textView];
    self.textView = textView;
    textView.font = [UIFont systemFontOfSize:12];
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.textColor = RGBA(37, 48, 59, 0.9);
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 6;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName:RGBA(37, 48, 59, 0.4),
                                 NSFontAttributeName:[UIFont systemFontOfSize:12],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:@"环境怎么样,服务是否周到?亲,快些下您的评论吧!(15字以上)" attributes:attributes];

    UILabel *textLable = [[UILabel alloc] initWithFrame:CGRectMake(textView.x, inputView.height - 20, textView.width, 20)];
    self.textLable = textLable;
    textLable.text = @"还可以输入120字";
    textLable.textColor = RGBA(37, 48, 59, 0.4);
    textLable.font = [UIFont systemFontOfSize:12];
    textLable.textAlignment = NSTextAlignmentRight;
    [inputView addSubview:textLable];
    [self.view addSubview:inputView];

然后实时监控更改提示文字

if (textView.text.length < 120) {
        self.textLable.text = [NSString stringWithFormat:@"还可以输入%zd字",120 - textView.text.length];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值