Contents

조회 수 3889 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
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


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
735 Develop [python][django] request.cookie 읽어오기 ㅋㅋㅋ (쓰기) hooni 2019.12.06 3685
734 Develop [ios] Locale Identifiers file hooni 2018.11.29 3717
733 Develop [python] 파라미터 앞에 *, ** 의 의미? (*args, **kwargs) hooni 2019.11.22 3786
732 Develop [펌] 게임 엔진 만든거 공개합니다. hooni 2015.02.21 3789
731 Develop [js] jQuery, Javascript 모바일(스마트폰) 판단하는 방법 hooni 2015.04.26 3793
730 Develop [ios] iOS앱의 Xcode 빌드 과정 file hooni 2015.01.03 3811
729 Develop [c] 셀프 넘버(Self Number) 구하기 1 hooni 2016.09.09 3826
728 Develop [ubuntu] 우분투 18.04에 PHP5 설치하기 hooni 2020.11.14 3836
727 Develop [coding] Find all anagrams in a string hooni 2017.06.27 3850
726 Develop [ios] Xcode cannot run using the selected device hooni 2014.08.14 3864
» Develop [ios] TextField 특정 문자만 사용하도록 하기 hooni 2014.06.30 3889
724 Develop [ios] Objective-C Types & Storage Capacity hooni 2015.07.22 3916
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 71 Next
/ 71