本文主要是介绍[IOS]UITextField限制字符输入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[IOS]UITextField限制字符输入
DEMO地址:http://download.csdn.net/detail/u012881779/8884627
只需要输入数字的情况,可以选择输入键盘来限制输入字符;而对于只需要输入字母或只需输入数字与字母这类的情况,没法用键盘限制输入字符,于是便有了下文。。。
#import "ViewController.h"#define DEF_CARD @"1234567890Xx"
/// 只能输入数字
#define DEF_NUMBER @"1234567890"
/// 只能输入邮箱需要字符
#define DEF_MAIL @"1234567890@.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/// 只能输入数字、字母、汉字(即限制输入特殊字符,用这种方法虽然比较笨,但还是有优点的,当不需要限制某些特殊字符时直接去掉就OK了)
#define DEF_NUMBERCHARCHINA [self specialSymbolsAction]@interface ViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;@end@implementation ViewController#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//名字if(textField.tag == 1000){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_NUMBERCHARCHINA]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(basicTest){if([string isEqualToString:@""]){return YES;}return NO;}//电话号码 纯数字可以使用数字键盘Number Pad}else if(textField.tag == 1001){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_NUMBER]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(!basicTest){return NO;}//身份证号}else if(textField.tag == 1002){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_CARD]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(!basicTest){return NO;}//邮箱 使用键盘E-mail Address}else if(textField.tag == 1003){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_MAIL]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(!basicTest){return NO;}}return YES;
}/// 完成编辑
- (void)textFieldDidEndEditing:(UITextField *)textField{NSString *fieldText = textField.text;NSString *changeField = @"";NSMutableArray *fieldArr = [[NSMutableArray alloc] init];for (int j = 0; j<fieldText.length; j++) {NSString *subStr = [fieldText substringWithRange:NSMakeRange(j, 1)];[fieldArr addObject:subStr];}for (int i = 0; i < fieldArr.count ; i ++) {NSString *string = [fieldArr objectAtIndex:i];//名字if(textField.tag == 1000){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_NUMBERCHARCHINA]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(!basicTest){changeField = [NSString stringWithFormat:@"%@%@",changeField,string];}//电话号码}else if(textField.tag == 1001){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_NUMBER]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(basicTest){changeField = [NSString stringWithFormat:@"%@%@",changeField,string];}//身份证号}else if(textField.tag == 1002){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_CARD]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(basicTest){changeField = [NSString stringWithFormat:@"%@%@",changeField,string];}//邮箱}else if(textField.tag == 1003){NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_MAIL]invertedSet];NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];BOOL basicTest = [string isEqualToString:filtered];if(basicTest){changeField = [NSString stringWithFormat:@"%@%@",changeField,string];}}}textField.text = changeField;
}/// 特殊符号
- (NSString *)specialSymbolsAction{//数学符号NSString *matSym = @" ﹢﹣×÷±/=≌∽≦≧≒﹤﹥≈≡≠=≤≥<>≮≯∷∶∫∮∝∞∧∨∑∏∪∩∈∵∴⊥∥∠⌒⊙√∟⊿㏒㏑%‰⅟½⅓⅕⅙⅛⅔⅖⅚⅜¾⅗⅝⅞⅘≂≃≄≅≆≇≈≉≊≋≌≍≎≏≐≑≒≓≔≕≖≗≘≙≚≛≜≝≞≟≠≡≢≣≤≥≦≧≨≩⊰⊱⋛⋚∫∬∭∮∯∰∱∲∳%℅‰‱øØπ";//标点符号NSString *punSym = @"。,、':∶;?‘’“”〝〞ˆˇ﹕︰﹔﹖﹑·¨….¸;!´?!~—ˉ|‖"〃`@﹫¡¿﹏﹋﹌︴々﹟#﹩$﹠&﹪%*﹡﹢﹦﹤‐ ̄¯―﹨ˆ˜﹍﹎+=<__-ˇ~﹉﹊()〈〉‹›﹛﹜『』〖〗[]《》〔〕{}「」【】︵︷︿︹︽_﹁﹃︻︶︸﹀︺︾ˉ﹂﹄︼❝❞!():,'[]{}^・.·.•#^*+=\<>&§⋯`-–/—|\"\\";//单位符号*·NSString *unitSym = @"°′″$¥〒¢£%@℃℉﹩﹪‰﹫㎡㏕㎜㎝㎞㏎m³㎎㎏㏄º○¤%$º¹²³";//货币符号NSString *curSym = @"₽€£Ұ₴$₰¢₤¥₳₲₪₵元₣₱฿¤₡₮₭₩ރ円₢₥₫₦zł﷼₠₧₯₨Kčर₹ƒ₸¢";//制表符NSString *tabSym = @"─ ━│┃╌╍╎╏┄ ┅┆┇┈ ┉┊┋┌┍┎┏┐┑┒┓└ ┕┖┗ ┘┙┚┛├┝┞┟┠┡┢┣ ┤┥┦┧┨┩┪┫┬ ┭ ┮ ┯ ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻┼ ┽ ┾ ┿ ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╪ ╫ ╬═║╒╓╔ ╕╖╗╘╙╚ ╛╜╝╞╟╠ ╡╢╣╤ ╥ ╦ ╧ ╨ ╩ ╳╔ ╗╝╚ ╬ ═ ╓ ╩ ┠ ┨┯ ┷┏ ┓┗ ┛┳ ⊥ ﹃ ﹄┌ ╮ ╭ ╯╰";return [NSString stringWithFormat:@"%@%@%@%@%@",matSym,punSym,unitSym,curSym,tabSym];
}/**常用符号大全*特殊符号 编号序号 数学符号 爱心符号 标点符号 单位符号 货币符号 箭头符号 符号图案 希腊字母 俄语字母*汉语拼音 中文字符 日语字符 制表符 皇冠符号*//*//特殊符号♠♣♧♡♥❤❥❣♂♀✲☀☼☾☽◐◑☺☻☎☏✿❀№↑↓←→√×÷★℃℉°◆◇⊙■□△▽¿½☯✡㍿卍卐♂♀✚〓㎡♪♫♩♬㊚㊛囍㊒㊖Φ♀♂‖$@*&#※卍卐Ψ♫♬♭♩♪♯♮⌒¶∮‖€£¥$//编号序号①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳⓪❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ⒜⒝⒞⒟⒠⒡⒢⒣⒤⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵//数学符号﹢﹣×÷±/=≌∽≦≧≒﹤﹥≈≡≠=≤≥<>≮≯∷∶∫∮∝∞∧∨∑∏∪∩∈∵∴⊥∥∠⌒⊙√∟⊿㏒㏑%‰⅟½⅓⅕⅙⅛⅔⅖⅚⅜¾⅗⅝⅞⅘≂≃≄≅≆≇≈≉≊≋≌≍≎≏≐≑≒≓≔≕≖≗≘≙≚≛≜≝≞≟≠≡≢≣≤≥≦≧≨≩⊰⊱⋛⋚∫∬∭∮∯∰∱∲∳%℅‰‱øØπ//爱心符号♥❣ღ♠♡♤❤❥//标点符号。,、':∶;?‘’“”〝〞ˆˇ﹕︰﹔﹖﹑·¨….¸;!´?!~—ˉ|‖"〃`@﹫¡¿﹏﹋﹌︴々﹟#﹩$﹠&﹪%*﹡﹢﹦﹤‐ ̄¯―﹨ˆ˜﹍﹎+=<__-ˇ~﹉﹊()〈〉‹›﹛﹜『』〖〗[]《》〔〕{}「」【】︵︷︿︹︽_﹁﹃︻︶︸﹀︺︾ˉ﹂﹄︼❝❞!():,'[]{}^・.·.•#^*+=\<>&§⋯`-–/—|\"\\//单位符号°′″$¥〒¢£%@℃℉﹩﹪‰﹫㎡㏕㎜㎝㎞㏎m³㎎㎏㏄º○¤%$º¹²³//货币符号₽€£Ұ₴$₰¢₤¥₳₲₪₵元₣₱฿¤₡₮₭₩ރ円₢₥₫₦zł﷼₠₧₯₨Kčर₹ƒ₸¢//箭头符号
↑↓←→↖↗↘↙↔↕➻➼➽➸➳➺➻➴➵➶➷➹▶►▷◁◀◄«»➩➪➫➬➭➮➯➱⏎➲➾➔➘➙➚➛➜➝➞➟➠➡➢➣➤➥➦➧➨↚↛↜↝↞↟↠↠↡↢↣↤↤↥↦↧↨⇄⇅⇆⇇⇈⇉⇊⇋⇌⇍⇎⇏⇐⇑⇒⇓⇔⇖⇗⇘⇙⇜↩↪↫↬↭↮↯↰↱↲↳↴↵↶↷↸↹☇☈↼↽↾↿⇀⇁⇂⇃⇞⇟⇠⇡⇢⇣⇤⇥⇦⇧⇨⇩⇪↺↻⇚⇛♐//符号图案✐✎✏✑✒✍✉✁✂✃✄✆✉☎☏☑✓✔√☐☒✗✘ㄨ✕✖✖☢☠☣✈★☆✡囍㍿☯☰☲☱☴☵☶☳☷☜☞☝✍☚☛☟✌♤♧♡♢♠♣♥♦☀☁☂❄☃♨웃유❖☽☾☪✿♂♀✪✯☭➳卍卐√×■◆●○◐◑✙☺☻❀⚘♔♕♖♗♘♙♚♛♜♝♞♟♧♡♂♀♠♣♥❤☜☞☎☏⊙◎☺☻☼▧▨♨◐◑↔↕▪▒◊◦▣▤▥▦▩◘◈◇♬♪♩♭♪の★☆→あぃ£Ю〓§♤♥▶¤✲❈✿✲❈➹☀☂☁【】┱┲❣✚✪✣✤✥✦❉❥❦❧❃❂❁❀✄☪☣☢☠☭ღ▶▷◀◁☀☁☂☃☄★☆☇☈⊙☊☋☌☍ⓛⓞⓥⓔ╬『』∴☀♫♬♩♭♪☆∷﹌の★◎▶☺☻►◄▧▨♨◐◑↔↕↘▀▄█▌◦☼♪の☆→♧ぃ£❤▒▬♦◊◦♠♣▣۰•❤•۰►◄▧▨♨◐◑↔↕▪▫☼♦⊙●○①⊕◎Θ⊙¤㊣★☆♀◆◇◣◢◥▲▼△▽⊿◤◥✐✌✍✡✓✔✕✖♂♀♥♡☜☞☎☏⊙◎☺☻►◄▧▨♨◐◑↔↕♥♡▪▫☼♦▀▄█▌▐░▒▬♦◊◘◙◦☼♠♣▣▤▥▦▩◘◙◈♫♬♪♩♭♪✄☪☣☢☠♯♩♪♫♬♭♮☎☏☪♈ºº₪¤큐«»™♂✿♥ ◕‿-。 。◕‿◕。//希腊字母ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζνξοπρσηθικλμτυφχψω//俄语字母АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя//汉语拼音āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜüêɑńňɡㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦㄧㄨㄩ//中文字符零壹贰叁肆伍陆柒捌玖拾佰仟万亿吉太拍艾分厘毫微卍卐卄巜弍弎弐朤氺曱甴囍兀々〆のぁ〡〢〣〤〥〦〧〨〩㊎㊍㊌㊋㊏㊚㊛㊐㊊㊣㊤㊥㊦㊧㊨㊒㊫㊑㊓㊔㊕㊖㊗㊘㊜㊝㊞㊟㊠㊡㊢㊩㊪㊬㊭㊮㊯㊰㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉//日文平假名片假名ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろゎわゐゑをんゔゕゖァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶヷヸヹヺ・ーヽヾヿ゠ㇰㇱㇲㇳㇴㇵㇶㇷㇸㇹㇺㇻㇼㇽㇾㇿ//制表符─ ━│┃╌╍╎╏┄ ┅┆┇┈ ┉┊┋┌┍┎┏┐┑┒┓└ ┕┖┗ ┘┙┚┛├┝┞┟┠┡┢┣ ┤┥┦┧┨┩┪┫┬ ┭ ┮ ┯ ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻┼ ┽ ┾ ┿ ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╪ ╫ ╬═║╒╓╔ ╕╖╗╘╙╚ ╛╜╝╞╟╠ ╡╢╣╤ ╥ ╦ ╧ ╨ ╩ ╳╔ ╗╝╚ ╬ ═ ╓ ╩ ┠ ┨┯ ┷┏ ┓┗ ┛┳ ⊥ ﹃ ﹄┌ ╮ ╭ ╯╰//皇冠符号♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟*/@end
示意图:
这篇关于[IOS]UITextField限制字符输入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!