Develop
2014.06.30 11:13
[ios] TextField 특정 문자만 사용하도록 하기
조회 수 2747 댓글 0
Xcode의 TextField 사용할때 특정 문자만 입력 받도록 하기 위해서는 다음과 같이 한다.
예) 숫자와 영문자만 입력 받기
#define LEGAL_TEXT @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LEGAL_TEXT] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return [string isEqualToString:filtered]; }
예2) 숫자와 소수점만 입력 받기 (소수점이 입력된 뒤에는 숫자만 입력 받는다)
키패드 타입을 변경
entryField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; #define NUMBERS @"0123456789" #define NUMBERSPERIOD @"0123456789." - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs; NSString *filtered; // Check for period if ([entryField.text rangeOfString:@"."].location == NSNotFound) { cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD] invertedSet]; filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return [string isEqualToString:filtered]; } // Period is in use cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet]; filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return [string isEqualToString:filtered]; }
[출처] http://comxp.tistory.com/250
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
777 | Develop | [linux] CentOS Apache 웹서버에 HTTPS 적용 | hooni | 2015.10.23 | 908 |
776 | System/OS | [linux] CentOS Apache Httpd에 https 적용 | hooni | 2014.03.05 | 4576 |
775 | System/OS | [linux] CentOS 6.x Cati 설치 (yum) | hooni | 2014.01.17 | 48657 |
774 | System/OS | [linux] CentOS 6.5 에서 "Bringing up interface eth0: Determining if ip address 121.78.127.197 is already in use for device eth0..." | hooni | 2014.04.05 | 4955 |
773 | System/OS | [linux] awk 명령어 | hooni | 2014.03.11 | 5028 |
772 | System/OS | [linux] APM(apache, php, mysql) + gd 설치순서.. | hooni | 2003.04.23 | 18920 |
771 | System/OS | [linux] apache, php, jsp 환경설정하기.. | hooni | 2003.04.23 | 15295 |
770 | System/OS | [linux] /etc/fstab 설정 방법.. ㅋㅋ | hooni | 2013.04.23 | 12140 |
769 | Etc | [link] 유용한 사이트 링크. | hooni | 2013.08.19 | 71557 |
768 | Etc | [link] iOS(아이폰) 개발 관련 ㅋㅋ | hooni | 2013.04.23 | 21538 |
767 | Develop | [lego] 세그웨이 이것만 볼것.. ㅎㅎ | hooni | 2013.04.23 | 33572 |
766 | Develop | [kotlin] 코틀린 안드로이드 앱 버전/빌드 정보 | hooni | 2020.12.15 | 968 |