欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

 更新時(shí)間:2017年10月11日 09:29:10   投稿:lqh  
這篇文章主要介紹了IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下

IOS中UITextView或UITextField字?jǐn)?shù)限制的實(shí)現(xiàn)

UITextView或UITextField字?jǐn)?shù)限制,輸入時(shí)的限制,復(fù)制粘貼時(shí)的限制

字?jǐn)?shù)限制有三種方法

在代理方法

“- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string”

“- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text”

中實(shí)現(xiàn)兩種方法

方法1(只能在輸入時(shí)限制,復(fù)制粘貼時(shí)無法限制)

if (range.location > MaxCharacterNumber - 1)
{
  textField.text = [textField.text substringToIndex:MaxCharacterNumber];
  return NO;
}

方法2(輸入及復(fù)制粘貼時(shí)均可限制)

NSString *temp = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (temp.length > MaxCharacterNumber)
{
  textField.text = [temp substringToIndex:MaxCharacterNumber];
  return NO;
}

在代理方法

“- (void)textViewDidChange:(UITextView *)textView”

中實(shí)現(xiàn)一種方法

方法3(復(fù)制粘貼時(shí)均可限制)

NSString *textString = textView.text;
if (textString.length > MaxCharacterNumbers + 1)
{
  textView.text = [textString substringToIndex:MaxCharacterNumbers];
  return;
}

注意:

“NSString *temp = [textField.text stringByReplacingCharactersInRange:range withString:string];”

為字符范圍替換為指定的字符串,返回新的字符串。

如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論