{"id":219,"date":"2015-05-20T13:33:32","date_gmt":"2015-05-20T05:33:32","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=219"},"modified":"2016-07-14T22:52:44","modified_gmt":"2016-07-14T14:52:44","slug":"ios%e5%88%b6%e4%bd%9c%e8%87%aa%e5%ae%9a%e4%b9%89%e6%95%b0%e5%ad%97%e9%94%ae%e7%9b%98","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2015\/05\/20\/133332219.html","title":{"rendered":"iOS\u5236\u4f5c\u81ea\u5b9a\u4e49\u6570\u5b57\u952e\u76d8"},"content":{"rendered":"<p><strong>\u4e00\u3001\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>\u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u7684\u8f93\u5165\u90fd\u662f\u6709\u786c\u6027\u8981\u6c42\u7684\uff0c\u6bd4\u5982\u53ea\u80fd\u8f93\u5165\u6570\u5b57\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u6307\u5b9a\u8f93\u5165\u6846\u7684\u952e\u76d8\u6a21\u5f0f\uff08keyboardType\uff09\uff0c\u53ef\u7cfb\u7edf\u63d0\u4f9b\u7ed9\u6211\u7684\u952e\u76d8\u79cd\u7c7b\u8fd8\u662f\u975e\u5e38\u6709\u9650\u7684\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">typedef NS_ENUM(NSInteger, UIKeyboardType) {\n????UIKeyboardTypeDefault,??????????????? \/\/ Default type for the current input method.\n????UIKeyboardTypeASCIICapable,?????????? \/\/ Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active\n????UIKeyboardTypeNumbersAndPunctuation,? \/\/ Numbers and assorted punctuation.\n????UIKeyboardTypeURL,??????????????????? \/\/ A type optimized for URL entry (shows . \/ .com prominently).\n????UIKeyboardTypeNumberPad,????????????? \/\/ A number pad (0-9). Suitable for PIN entry.\n????UIKeyboardTypePhonePad,?????????????? \/\/ A phone pad (1-9, *, 0, #, with letters under the numbers).\n????UIKeyboardTypeNamePhonePad,?????????? \/\/ A type optimized for entering a person's name or phone number.\n????UIKeyboardTypeEmailAddress,?????????? \/\/ A type optimized for multiple email address entry (shows space @ . prominently).\n????UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),?? \/\/ A number pad with a decimal point.\n????UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),????? \/\/ A type optimized for twitter text entry (easy access to @ #)\n????UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),??? \/\/ A default keyboard type with URL-oriented addition (shows space . prominently).\n?\n????UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, \/\/ Deprecated\n?\n};<\/pre>\n<p><!--more-->\u6709\u65f6\u5019\u53ef\u80fd\u4e0d\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u8981\uff0c\u6bd4\u5982\u5728\u6570\u5b57\u952e\u76d8\u4e2d\u9700\u8981\u589e\u52a0\u4e00\u4e2a\u5b8c\u6210\u6309\u94ae\u7b49\u7b49\uff0c\u6211\u4eec\u5728\u73b0\u6709\u7684\u952e\u76d8\u4e0a\u52a8\u624b\u811a\u90fd\u65e0\u6cd5\u5b8c\u7f8e\u89e3\u51b3\u6211\u4eec\u6240\u9047\u5230\u7684\u95ee\u9898\uff0c\u6700\u597d\u7684\u529e\u6cd5\u5c31\u662f\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7b26\u5408\u6211\u4eec\u9700\u6c42\u7684\u952e\u76d8\u3002<\/p>\n<p><strong>\u4e8c\u3001\u793a\u4f8b\u6548\u679c<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-220\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-Simulator-Screen-Shot-2015\u5e745\u670820\u65e5-\u4e0b\u53481.39.56.png\" alt=\"iOS Simulator Screen Shot 2015\u5e745\u670820\u65e5 \u4e0b\u53481.39.56\" width=\"320\" height=\"480\" \/><\/p>\n<p>\u5982\u4f55\u80fd\u5b9e\u73b0\u8fd9\u79cd\u6548\u679c\u5462\uff0c\u5176\u5b9e\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">self.textField.keyboardType = SAKeyboardTypeNumber;<\/pre>\n<p><strong>\u4e09\u3001\u8bbe\u8ba1\u539f\u7406<\/strong><\/p>\n<p>3.1\u3001\u7c7b\u4f3c\u4e8eUITextField\u8fd9\u6837\u7684\u6587\u672c\u8f93\u5165\u63a7\u4ef6\uff0c\u4e00\u822c\u90fd\u6709\u4e00\u4e2ainputView\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u5c31\u662f\u7528\u6765\u6307\u5b9a\u81ea\u5b9a\u4e49\u952e\u76d8\u7684\uff0c\u8fd8\u6709\u4e00\u4e2ainputAccessoryView\u5c5e\u6027\uff0c\u7528\u6765\u5728\u952e\u76d8\u4e0a\u65b9\u589e\u52a0\u81ea\u5b9a\u4e49\u7684\u5de5\u5177\u6761\u6216\u63a7\u4ef6\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u952e\u76d8\u5c31\u4eceinputView\u8fd9\u4e2a\u5c5e\u6027\u4e0a\u505a\u6587\u7ae0\u3002<\/p>\n<p>3.2\u3001\u7136\u800c\u5728\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u6211\u53c8\u5e0c\u671b\u80fd\u4fdd\u6301\u7cfb\u7edf\u98ce\u683c\uff0c\u5373\u901a\u8fc7setKeyboardType:\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u6216\u8005\u6539\u53d8keyboardType\u8fd9\u4e2a\u679a\u4e3e\u503c\u6765\u8fbe\u5230\u6307\u5b9a\u81ea\u5b9a\u4e49\u952e\u76d8\u7684\u76ee\u7684\uff0c\u770b\u8d77\u6765\u4e00\u5207\u90fd\u662f\u90a3\u4e48\u7684\u81ea\u7136\uff0c\u5c31\u597d\u50cf\u6269\u5c55\u4e86\u7cfb\u7edf\u952e\u76d8\u7c7b\u578b\u8fd9\u79cd\u6548\u679c\uff0c\u6211\u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u79cd\u5c01\u88c5\u662f\u6700\u597d\u7684\uff0c\u4e0d\u7528\u6539\u53d8\u5f00\u53d1\u8005\u7684\u4f7f\u7528\u4e60\u60ef\uff01<\/p>\n<p>3.3\u3001\u7531\u4e8e\u8981\u6539\u53d8setKeyboardType:\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u65e0\u6cd5\u901a\u8fc7Category\u5b9e\u73b0\uff0c\u53ea\u80fd\u81ea\u5b9a\u4e49\u4e00\u4e2aTextField\u7ee7\u627f\u81eaUITextField\u6765\u8986\u76d6\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5982\u679c\u4e0d\u60f3\u8fd9\u6837\u505a\uff0c\u90a3\u5c31\u53ea\u80fd\u901a\u8fc7inputView\u5c5e\u6027\u6765\u5b9e\u73b0\uff0c\u6211\u8fd9\u91cc\u53ea\u4ecb\u7ecd\u524d\u8005\uff0c\u6211\u8ba4\u4e3a\u91cd\u5199\u6ca1\u4ec0\u4e48\u4e0d\u597d\uff0c\u5e76\u4e14\u9879\u76ee\u4e0a\u5f88\u591a\u7cfb\u7edf\u7684\u7c7b\u90fd\u662f\u9700\u8981\u91cd\u5199\u7684\uff0c\u91cd\u5199\u53ef\u4ee5\u7edf\u4e00\u98ce\u683c\uff0c\u7edf\u4e00\u8c03\u6574\uff0c\u91cd\u5199\u8fd8\u53ef\u4ee5\u79ef\u7d2f\u81ea\u5df1\u7684\u4e00\u4e9b\u597d\u7528\u7684\u4e1c\u897f\uff0c\u6700\u7ec8\u53ef\u4ee5\u79ef\u7d2f\u51fa\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u5e93\uff01<\/p>\n<p>3.4\u3001\u53e6\u5916\u4e00\u70b9\uff0c\u8fd9\u4e2a\u952e\u76d8\u9700\u8981\u4e8b\u5148\u901a\u8fc7xib\u753b\u597d\uff0c\u6bcf\u4e00\u4e2a\u952e\u53ef\u4ee5\u4f7f\u7528\u6309\u94ae\u6765\u5b9e\u73b0\uff0c\u6392\u7248\u5e03\u5c40\u8fd9\u91cc\u4e0d\u505a\u592a\u591a\u8bf4\u660e\uff0c\u5b9e\u73b0\u8f93\u5165\u5219\u662f\u8c03\u7528TextField\u7684replaceRange:withText\u8fd9\u4e2a\u5c31\u53bb\u6765\u5b9e\u73b0<\/p>\n<p><strong>\u56db\u3001\u4ee3\u7801\u8bf4\u660e<\/strong><\/p>\n<p>SATextField.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SATextField.m\n\/\/? Test\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 15\/5\/16.\n\/\/? Copyright (c) 2015\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import \"SATextField.h\"\n#import \"SANumberKeyboard.h\"\n?\n@implementation SATextField\n?\n- (void)setKeyboardType:(SAKeyboardType)keyboardType\n{\n????switch (keyboardType) {\n????????case SAKeyboardTypeNumber:{\n????????????SANumberKeyboard *keyboard = [SANumberKeyboard keyboardWithTextField:self];\n????????????self.inputView = keyboard;\n?????????????\n????????}break;\n?????????????\n????????default: [super setKeyboardType:(UIKeyboardType)keyboardType]; break;\n????}\n}\n?\n@end<\/pre>\n<p>SANumberKeyboard.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/? SANumberKeyboard.m\n\/\/? Test\n\/\/\n\/\/? Created by \u4f59\u897f\u5b89 on 15\/5\/16.\n\/\/? Copyright (c) 2015\u5e74 Sian. All rights reserved.\n\/\/\n?\n#import \"SANumberKeyboard.h\"\n?\n@implementation SANumberKeyboard\n?\n+ (instancetype)keyboardWithTextField:(SATextField *)textField;\n{\n????SANumberKeyboard *keyboard = [[self alloc] init];\n????keyboard.textField = textField;\n????return keyboard;\n}\n?\n- (instancetype)init\n{\n????self = [[[NSBundle mainBundle] loadNibNamed:@\"SANumberKeyboard\" owner:nil options:nil] firstObject];\n????for (UIButton *btn in self.subviews) {\n????????if (btn.tag == 0) continue;\n????????UIImage *image = [self imageWithColor:[UIColor whiteColor]];\n????????UIImage *image1 = [self imageWithColor:[UIColor colorWithWhite:0.8 alpha:1.0]];\n????????if (btn.tag &lt;= 10)[btn setBackgroundImage:image forState:UIControlStateNormal]; ????????if (btn.tag &gt; 10)[btn setBackgroundImage:image1 forState:UIControlStateNormal];\n????????[btn addTarget:self action:@selector(buttonEven:) forControlEvents:UIControlEventTouchUpInside];\n????}\n????return self;\n}\n?\n\/\/\/ \u6307\u5b9a\u989c\u8272\u751f\u6210\u4e00\u5f20\u5927\u5c0f\u4e3a(1,1)\u7684\u56fe\u7247\n- (UIImage *)imageWithColor:(UIColor *)color\n{\n????CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);\n????UIGraphicsBeginImageContext(rect.size);\n????CGContextRef context = UIGraphicsGetCurrentContext();\n?????\n????CGContextSetFillColorWithColor(context, [color CGColor]);\n????CGContextFillRect(context, rect);\n?????\n????UIImage *image = UIGraphicsGetImageFromCurrentImageContext();\n????UIGraphicsEndImageContext();\n?????\n????return image;\n}\n?\n\/\/\/ \u5404\u6309\u952e\u4e8b\u4ef6\n- (void)buttonEven:(UIButton *)button\n{\n????UITextRange *textRange = self.textField.selectedTextRange;\n????NSString *string = @\"\";\n????switch (button.tag) {\n????????case? 1: string = @\"1\"; break;\n????????case? 2: string = @\"2\"; break;\n????????case? 3: string = @\"3\"; break;\n????????case? 4: string = @\"4\"; break;\n????????case? 5: string = @\"5\"; break;\n????????case? 6: string = @\"6\"; break;\n????????case? 7: string = @\"7\"; break;\n????????case? 8: string = @\"8\"; break;\n????????case? 9: string = @\"9\"; break;\n????????case 10: string = @\"0\"; break;\n????????case 11: string = @\".\"; break;\n????????case 12:{\n????????????if (textRange.isEmpty){\n????????????????UITextPosition *from = [self.textField positionFromPosition:textRange.start offset:-1];\n????????????????UITextPosition *to = [self.textField positionFromPosition:textRange.start offset:0];\n????????????????textRange = [self.textField textRangeFromPosition:from toPosition:to];\n????????????}\n????????}break;\n????????default:break;\n????}\n????[self.textField replaceRange:textRange withText:string];\n}\n?\n@end<\/pre>\n<p><strong>\u4e94\u3001\u6e90\u7801\u4e0b\u8f7d(Demo)<\/strong><\/p>\n<p><a href=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/SAKeyboard.zip\">SAKeyboard<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5e94\u7528\u573a\u666f \u5f88\u591a\u60c5\u51b5\u4e0b\u6211\u4eec\u7684\u8f93\u5165\u90fd\u662f\u6709\u786c\u6027\u8981\u6c42\u7684\uff0c\u6bd4\u5982\u53ea\u80fd\u8f93\u5165\u6570\u5b57\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u6307\u5b9a\u8f93\u5165\u6846\u7684\u952e\u76d8\u6a21\u5f0f\uff08keyboardType\uff09\uff0c\u53ef\u7cfb\u7edf\u63d0\u4f9b\u7ed9\u6211\u7684\u952e\u76d8\u79cd\u7c7b\u8fd8\u662f\u975e\u5e38\u6709\u9650\u7684\uff0c\u5982\u4e0b\u6240\u793a\uff1a typedef NS_ENUM(NSInteger, UIKeyboardType) { ????UIKeyboardTypeDefault,??????????????? \/\/ Default type for the current input method. ????UIKeyboardTypeASCIICapable,?????????? \/\/ Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active ????UIKeyboardTypeNumbersAndPunctuation,? \/\/ Numbers and assorted punctuation. ????UIKeyboardTypeURL,??????????????????? \/\/ A type optimized for URL entry (shows . \/ .com prominently). ????UIKeyboardTypeNumberPad,????????????? \/\/ A number pad (0-9). Suitable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[15,17],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-project","tag-module","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}