UITextField的代理方法:textField:shouldChangeCharactersInRange:replacementString,

來源:互聯網
上載者:User

UITextField的代理方法:textField:shouldChangeCharactersInRange:replacementString,

原文連結: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:02016-11-25 14:51:42.799856 OnlyNumberTextField[2385:416738] string:➏---Range's location:1,Range's length:02016-11-25 14:51:43.373470 OnlyNumberTextField[2385:416738] string:我---Range's location:0,Range's length:22016-11-25 14:51:45.202028 OnlyNumberTextField[2385:416738] string:➐---Range's location:1,Range's length:02016-11-25 14:51:45.603080 OnlyNumberTextField[2385:416738] string:➍---Range's location:2,Range's length:02016-11-25 14:51:45.800381 OnlyNumberTextField[2385:416738] string:➍---Range's location:3,Range's length:02016-11-25 14:51:46.357566 OnlyNumberTextField[2385:416738] string:是---Range's location:1,Range's length:32016-11-25 14:51:47.067459 OnlyNumberTextField[2385:416738] string:➋---Range's location:2,Range's length:02016-11-25 14:51:47.701954 OnlyNumberTextField[2385:416738] string:➍---Range's location:3,Range's length:02016-11-25 14:51:47.865956 OnlyNumberTextField[2385:416738] string:➌---Range's location:4,Range's length:02016-11-25 14:51:48.068942 OnlyNumberTextField[2385:416738] string:➏---Range's location:5,Range's length:02016-11-25 14:51:48.148413 OnlyNumberTextField[2385:416738] string:➍---Range's location:6,Range's length:02016-11-25 14:51:59.334791 OnlyNumberTextField[2385:416738] string:程---Range's location:2,Range's length:52016-11-25 14:52:00.459496 OnlyNumberTextField[2385:416738] string:序---Range's location:3,Range's length:02016-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:12016-11-25 14:54:16.602975 OnlyNumberTextField[2385:416738] string:---Range's location:3,Range's length:12016-11-25 14:54:17.049679 OnlyNumberTextField[2385:416738] string:---Range's location:2,Range's length:12016-11-25 14:54:17.466124 OnlyNumberTextField[2385:416738] string:---Range's location:1,Range's length:12016-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

 

結束。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.