본문 바로가기

iPhone

원하는 문자만 입력 받기

LEGAL 있는 문자만 입력 받는 방법

#define LEGAL @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "

textField delegate shouldChangeCharactersInRange 아래와 같이 구현

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

NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

return [string isEqualToString:filtered];

}

출처 : http://blogs.oreilly.com/iphone/2009/01/defining-legal-input-character.html 

'iPhone' 카테고리의 다른 글