iOS 中 UITextView 限制最大輸入字數


 

顯示UITextView最大輸入字數的代碼

 

 1 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
 2 {
 3     //如果是刪除減少字數,都返回允許修改
 4     if ([text isEqualToString:@""]) {
 5         return YES;
 6     }
 7     if (range.location>= MaxCount)
 8     {
 9         return NO;
10     }
11     else
12     {
13         return YES;
14     }
15 }
16 - (void)textViewDidChange:(UITextView *)textView {
17     [self updateRemainCount];
18 }
19 - (void)updateRemainCount {
20     long count = MAX(0, (int)(MaxCount - self.textView.text.length));
21     self.lbRemainCount.text = [NSString stringWithFormat:@"當前可輸入文字:%ld",count];
22 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM