textView和textField 限制文字输入字数

/**
 *  计算输入的字数,超出规定字数不允许输入,这个是代理方法
 *
 * 
  @param  textView  文字视图
 */

- (
void )textViewDidChange:( UITextView  *)textView
{
   
  NSString   * nsTextContent=textView. text ;
   
  long  existTextNum = [nsTextContent  length ];
   
  if  (existTextNum >  TextViewMaxLength ) {
        nsTextContent = [nsTextContent
  substringToIndex : TextViewMaxLength ];
        existTextNum = [nsTextContent
  length ];
    }
   
  self . currentCountLabel . text  = [ NSString  stringWithFormat : @"%lu" ,( unsigned  long )existTextNum];
   
  self . replyTextView . text  = nsTextContent;
}


#pragma mark - textField 限制输入字数方法
//这个是addTarget写的方法
- ( IBAction )textFieldDidChanged:( UITextField  *)sender {
   
   
  NSString   * textStr = sender. text ;
   
  long  existTextNum = [textStr  length ];
   
  if  (existTextNum >  200 ) {
        textStr = [textStr
  substringToIndex : 200 ];
        existTextNum = [textStr
  length ];
    }
   
    sender.
text  = textStr;
}

你可能感兴趣的:(ios,限制字数,文字输入限制)