原文鏈接:http://www.cnblogs.com/zhanggui/p/6101813.html
這個我在開發的過程中用到的次數最多,因此這里就簡單對其進行分析。先看看Command+點擊 彈出的內容解釋:
它的解釋大概意思如下:告訴代理方法指定的text應不應該改變。textfiled會在用戶輸入內容改變的情況下調用。使用這個方法來驗證使用時用戶輸入的類型。例如,你可以使用這個方法來讓用戶只是輸入數字,而沒有其他字符。
它的string參數:用來在指定范圍替換的字符。在輸入的過程中,這個參數只包含單個輸入的字符,比如要輸入一句我是程序員,可以看一下結果:
2016-11-25 14:51:42.602606 OnlyNumberTextField[2385:416738] string:➒---Range's location:0,Range's length:0 2016-11-25 14:51:42.799856 OnlyNumberTextField[2385:416738] string:➏---Range's location:1,Range's length:0 2016-11-25 14:51:43.373470 OnlyNumberTextField[2385:416738] string:我---Range's location:0,Range's length:2 2016-11-25 14:51:45.202028 OnlyNumberTextField[2385:416738] string:➐---Range's location:1,Range's length:0 2016-11-25 14:51:45.603080 OnlyNumberTextField[2385:416738] string:➍---Range's location:2,Range's length:0 2016-11-25 14:51:45.800381 OnlyNumberTextField[2385:416738] string:➍---Range's location:3,Range's length:0 2016-11-25 14:51:46.357566 OnlyNumberTextField[2385:416738] string:是---Range's location:1,Range's length:3 2016-11-25 14:51:47.067459 OnlyNumberTextField[2385:416738] string:➋---Range's location:2,Range's length:0 2016-11-25 14:51:47.701954 OnlyNumberTextField[2385:416738] string:➍---Range's location:3,Range's length:0 2016-11-25 14:51:47.865956 OnlyNumberTextField[2385:416738] string:➌---Range's location:4,Range's length:0 2016-11-25 14:51:48.068942 OnlyNumberTextField[2385:416738] string:➏---Range's location:5,Range's length:0 2016-11-25 14:51:48.148413 OnlyNumberTextField[2385:416738] string:➍---Range's location:6,Range's length:0 2016-11-25 14:51:59.334791 OnlyNumberTextField[2385:416738] string:程---Range's location:2,Range's length:5 2016-11-25 14:52:00.459496 OnlyNumberTextField[2385:416738] string:序---Range's location:3,Range's length:0 2016-11-25 14:52:01.760261 OnlyNumberTextField[2385:416738] string:員---Range's location:4,Range's length:0
看到這個結果凌亂了,還有黑圈數字啥的。那個是占位還沒有輸入內容的時候的字母提示。然后輸入的打印結果就是上面的內容。如果是粘貼,這個string還可能包含更多的字符。當用戶刪除一個或者多個字符的時候:
2016-11-25 14:54:16.152642 OnlyNumberTextField[2385:416738] string:---Range's location:4,Range's length:1 2016-11-25 14:54:16.602975 OnlyNumberTextField[2385:416738] string:---Range's location:3,Range's length:1 2016-11-25 14:54:17.049679 OnlyNumberTextField[2385:416738] string:---Range's location:2,Range's length:1 2016-11-25 14:54:17.466124 OnlyNumberTextField[2385:416738] string:---Range's location:1,Range's length:1 2016-11-25 14:54:18.436184 OnlyNumberTextField[2385:416738] string:---Range's location:0,Range's length:1
這個string就是空的。
這里的返回值BOOL:YES表示指定的text范圍應該被替換成string,否則還是保持原樣。
下面舉兩個例子說明:
第一個:用來驗證只能輸入數字:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]; NSString *filteredStr = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""]; if ([string isEqualToString:filteredStr]) { return YES; } return NO; }
這里用到了NSCharacterSet類,還有一個array的方法componentsJoinedByString:。該方法的作用是將數組內容進行組合,然后生成一個字符串比如:
NSArray *pathArray = [NSArray arrayWithObjects:@"here", @"be", @"dragons", nil]; NSLog(@"%@",[pathArray componentsJoinedByString:@""]);
輸出結果就是:herebedragons
如果ByString后面是@" ",結果就會是:here be dragons。這里還有一個NSString的方法:
- (NSArray<NSString *> *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
該方法的而作用是通過set來進行分割字符串,返回分割后的數組。例如:
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"0123456"]; NSString *nam = @"1g2h45j3d688"; NSLog(@"%@",[nam componentsSeparatedByCharactersInSet:set]);
結果如下:
2016-11-25 15:25:56.615351 OnlyNumberTextField[2403:421474] ( "", g, h, "", j, d, 88 )
如果上面的set 調用 invertedSet輸出結果如下:
2016-11-25 15:27:29.218759 OnlyNumberTextField[2405:421836] ( 1, 2, 45, 3, 6, "", "" )
因此以下的代碼:
NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]; NSString *nam = @"1g2h45j3d688"; NSArray *arr = [nam componentsSeparatedByCharactersInSet:set]; NSLog(@"%@",[arr componentsJoinedByString:@""]);
輸出結果就是:12453688
因此就拿只能輸入數字這個方法而言,方法里面先設置了一個反轉的set,然后將將要替換的字符進行過濾,如果過濾后還是和原來一樣,說明滿足過濾標准,就替換原有字符。如果不符合原有標准就在直接返回NO,也就意味着不替換原有字符,保持原樣。
此外,還有只能輸入字母和數字的判斷方法:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet]; NSString *filteredStr = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""]; if ([string isEqualToString:filteredStr]) { return YES; } return NO; }
和只輸入數字的方法差不多,只是過濾條件有所差異。
比如你還想只輸入字母,你可以直接修改set為:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
結束。