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

//剩余可输入多少个字+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;
    原文作者:ShevaKuilin
    原文地址: https://segmentfault.com/a/1190000004537869
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞