[分享]iOS开发-剩余可输入多少个字+textView上的label伪装成placeHolder

本文介绍了一种在文本视图中实现剩余输入字数提示与超出限制范围显示的改进方法,通过使用标签替代直接操作文本视图的text属性来实现更优雅的用户体验。此外,文章还讨论了如何在文本输入过程中实时更新提示信息,以帮助用户更好地控制输入长度。
//剩余可输入多少个字+textView上的label伪装成placeHolder
-(void)textViewDidChange:(UITextView *)textView{
NSInteger inputLength = 40 - self.textView.text.length;    
NSString * remainder = [NSString stringWithFormat:@"剩余输入%@字", @(inputLength)];    
NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:remainder];    
[attString addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:MyFont(14)} range:[remainder rangeOfString:[NSString stringWithFormat:@"%@", @(inputLength)]]];    
self.changeLabel.attributedText = attString;
 }
 
-(IBAction)touchOther:(UIControl *)sender {    
[self.textView resignFirstResponder];
 }
 
-(void)textViewDidEndEditing:(UITextView *)textView{   
 [self.view endEditing:YES];    
if (self.textView.text.length > 0) {        
self.promptLabel.hidden = YES;   
 } else {        
    self.promptLabel.hidden = NO;    
}
}

-(void)textViewDidBeginEditing:(UITextView *)textView{    
  self.promptLabel.hidden = YES;  

 }

//其实还有更好的方式,是在-(void)textViewDidChange:(UITextView *)textView中进行判断是否隐藏,关于这一类的伪装placeHolder,尽量都用label来进行,不要直接选择对textView本身的text进行操作

//超出textField限制范围时的label实时显示
NSString * remainder = [NSString stringWithFormat:@"已超出限定范围%@字", @(labs(inputLength))];
NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:remainder];
[attString addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:MyFont(14)} range:[remainder rangeOfString:[NSString stringWithFormat:@"%@", @(labs(inputLength))]]];
self.changeLabel.attributedText = attString;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值