Created
February 24, 2014 12:04
-
-
Save satoshin2071/9187188 to your computer and use it in GitHub Desktop.
[iOS]UITextViewでの入力チェック
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
#define MAX_MEMO_LENGTH 32 | |
#define MEMO_REGEX @"^[ぁ-んァ-ヶーa-zA-Z0-9一-龠0-9、。,. ]+$" | |
. | |
. | |
.. | |
/* | |
最後に頭と末尾の空白文字取り除くのが望ましい | |
NSMutableString *str = [NSMutableString stringWithString:_textView.text]; | |
CFStringTrimWhitespace((__bridge CFMutableStringRef)str); | |
_textView.text = nil; | |
*/ | |
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text | |
{ | |
NSString *str = [textView.text stringByReplacingCharactersInRange:range withString:text]; | |
NSError *error = nil; | |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:MEMO_REGEX options:NSRegularExpressionCaseInsensitive error:&error]; | |
NSTextCheckingResult *matches = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])]; | |
//TODO:アラート文言? | |
// 最大数チェック | |
if ([str length] > MAX_MEMO_LENGTH) { | |
[UIAlertView showAlertViewWithTitle:@"エラー" message:@"申し訳ございません。\n文字数オーバーです" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:nil]; | |
return NO; | |
// 正規表現でチェック | |
}else if([str length] > 0 && matches==nil){ | |
[UIAlertView showAlertViewWithTitle:@"エラー" message:@"申し訳ございません。\nあまり特殊な記号と改行は使えません。" cancelButtonTitle:@"OK" otherButtonTitles:nil handler:nil]; | |
return NO; | |
} | |
[self.navigationItem.leftBarButtonItem setEnabled:YES]; | |
return YES; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NSStringにするなら特に必要ないかも