Created
September 4, 2014 04:44
-
-
Save shingohry/8d7cb98b6323598ab2a6 to your computer and use it in GitHub Desktop.
limit textView text length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text | |
{ | |
// すでに入力されているテキストを取得、今回編集されたテキストをマージ | |
NSMutableString *textViewText = [textView.text mutableCopy]; | |
[textViewText replaceCharactersInRange:range withString:text]; | |
// 結果が文字数をオーバーしていないならYES,オーバーしている場合はNO | |
if ([textViewText length] <= self.maxTextLength) { | |
// 最大文字数以下 | |
return YES; | |
} else { | |
// 最大文字数より長い | |
return NO; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment