{"id":591,"date":"2014-07-11T10:29:13","date_gmt":"2014-07-11T02:29:13","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=591"},"modified":"2016-07-15T15:54:20","modified_gmt":"2016-07-15T07:54:20","slug":"uitextfield%e9%99%90%e5%88%b6%e4%b8%ad%e6%96%87%e8%be%93%e5%85%a5%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/07\/11\/102913591.html","title":{"rendered":"UITextField\u9650\u5236\u4e2d\u6587\u8f93\u5165\u957f\u5ea6"},"content":{"rendered":"<p><strong>\u9700\u6c42\uff1a\u8f93\u5165\u6846\u5185\u8f93\u5165\u6587\u5b57\u5b57\u6570\u9650\u5236\u572820\u5b57\u3002<\/strong><\/p>\n<p><strong>\u65b9\u6848\u4e00\uff1a\u91c7\u7528\u4e86UITextField\u4f5c\u4e3a\u6211\u7684\u8f93\u5165\u6846\u63a7\u4ef6\uff0c\u5e76\u4e14\u5728\u59d4\u6258\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string<\/pre>\n<p>\u4e2d\u5b9e\u73b0\u4e86\u5bf9\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9650\u5236\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">#define kMaxLength 20\n\n- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{\n\n   NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];\n\n   if (toBeString.length &gt; kMaxLength &amp;&amp; range.length!=1){\n\n       textField.text = [toBeString substringToIndex:kMaxLength];\n\n       return NO;\n\n   }\n   return YES;\n}<\/pre>\n<p><strong>\u65b9\u6848\u4e8c\uff1a\u6ce8\u518c\u8fd9\u4e2a\u89c2\u5bdf\u8005 UITextFieldTextDidChangeNotification<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];<\/pre>\n<p>\u7136\u540e\u5b9e\u73b0\u8fd9\u4e2a\u901a\u77e5\u65b9\u6cd5\u5373\u53ef\u3002<!--more--><\/p>\n<p>\u8fd9\u6837\u5b9e\u73b0\u7684\u7ed3\u679c\u662f\uff1a\u5bf9\u4e8e\u7eaf\u5b57\u7b26\u7684\u7edf\u8ba1\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u5f53\u8f93\u5165\u7684\u5b57\u7b26\u8d85\u8fc7\u9650\u5236\u65f6\u8f93\u5165\u6846\u4fbf\u622a\u53d6\u6700\u5927\u9650\u5236\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0c\u6709\u4e2a\u95ee\u9898\uff0c\u5f53\u4f7f\u7528\u62fc\u97f3\u8f93\u5165\u6cd5\u65f6\uff0c\u8be5\u59d4\u6258\u65b9\u6cd5\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570string\u63a5\u53d7\u7684\u662f\u8f93\u5165\u7684\u5b57\u6bcd\uff0c\u800c\u4e0d\u662f\u9009\u62e9\u7684\u6c49\u5b57\uff0c\u9020\u6210\u7684\u7ed3\u679c\u662f\uff0c\u5f53\u60f3\u8f93\u5165\u6587\u5b57\u201c\u6211\u5728\u7f16\u7a0b\u201d\uff0c\u8f93\u5165\u62fc\u97f3\u201cwozaibiancheng\u201d\uff0c\u6bcf\u8f93\u5165\u4e00\u4e2a\u5b57\u6bcd\u4fbf\u4f1a\u8fdb\u5165\u59d4\u6258\u65b9\u6cd5\uff0c\u7edf\u8ba1\u7684\u5b57\u7b26\u957f\u5ea6\u662f\u5b57\u6bcd\u7684\u957f\u5ea6\uff0c\u5b9e\u9645\u4e0a\u6c49\u5b57\u8fd8\u672a\u8d85\u8fc7\u9650\u5236\u957f\u5ea6\uff0c\u4f46\u662f\u5b57\u6bcd\u7684\u957f\u5ea6\u8d85\u8fc7\u4e86\u5bfc\u81f4\u65e0\u6cd5\u7ee7\u7eed\u8f93\u5165\u3002\u800c\u4e14\uff0c\u81f4\u547d\u7684\u662f\uff0c\u8fd9\u4e2a\u59d4\u6258\u65b9\u6cd5\u5e76\u4e0d\u54cd\u5e94\uff0c\u9009\u4e2d\u5019\u9009\u6c49\u5b57\u7684\u8fc7\u7a0b\uff0c\u8fd9\u5c31\u6ca1\u6709\u529e\u6cd5\u91cd\u65b0\u4fee\u6b63\u5b57\u7b26\u957f\u5ea6\u7684\u7edf\u8ba1\u4e86\u3002<\/p>\n<p><strong>\u5173\u952e\u95ee\u9898\uff1a<\/strong>\u5728\u4e2d\u6587\u8f93\u5165\u65f6\uff0c\u5907\u9009\u62fc\u97f3\u88ab\u8ba1\u7b97\u5230TextField\u7684Text\u4e2d\uff0c\u56e0\u6b64\u4e5f\u88ab\u8ba1\u7b97\u4e86\u957f\u5ea6\uff0c\u8fd9\u6837\u663e\u7136\u662f\u4e0d\u5408\u7406\u7684\uff0c\u53ef\u4eba\u5bb6\u5c31\u662f\u8fd9\u6837\u505a\u7684\u5440\uff0c\u53ef\u559c\u7684\u662f\u5907\u9009\u62fc\u97f3\u672c\u8eab\u7684ValueChange\u4e5f\u88ab\u76d1\u542c\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u7279\u6027\u6765\u5de7\u5999\u5730\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5373textField.markedTextRange\uff0c\u5982\u679c\u8be5\u5c5e\u6027\u8fd4\u56de\u7684UITextRange\u4e3anull\uff0c\u5219\u8bf4\u660e\u65e0\u5907\u9009\u62fc\u97f3\u5728\u8f93\u5165\u6cd5\u4e2d\uff0c\u7136\u540e\u5462\uff1f<strong>\u7136\u540e\u5c31\u53ea\u6709\u5728\u8be5\u5c5e\u6027\u4e3a\u7a7a\u65f6\u624d\u7edf\u8ba1\u6570\u5b57\u5c31\u53ef\u4ee5\u4e86\uff01<\/strong><\/p>\n<p>\u8981\u6211\u8bf4\u5f97\u518d\u660e\u663e\u4e00\u70b9\u5417\uff1f\u52a0\u4e2a\u5224\u65ad\u6761\u4ef6<\/p>\n<pre lang=\"objc\" line=\"1\">if (![textField markedTextRange]) {\n       \/\/ \u7edf\u8ba1\u6570\u5b57\u4ee3\u7801\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u8f93\u5165\u6846\u5185\u8f93\u5165\u6587\u5b57\u5b57\u6570\u9650\u5236\u572820\u5b57\u3002 \u65b9\u6848\u4e00\uff1a\u91c7\u7528\u4e86UITextField\u4f5c\u4e3a\u6211\u7684\u8f93\u5165\u6846\u63a7\u4ef6\uff0c\u5e76\u4e14\u5728\u59d4\u6258\u65b9\u6cd5\uff1a &#8211; (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string \u4e2d\u5b9e\u73b0\u4e86\u5bf9\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9650\u5236\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a #define kMaxLength 20 &#8211; (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (toBeString.length &gt; kMaxLength &amp;&amp; range.length!=1){ textField.text = [toBeString substringToIndex:kMaxLength]; return NO; } return YES; } \u65b9\u6848\u4e8c\uff1a\u6ce8\u518c\u8fd9\u4e2a\u89c2\u5bdf\u8005 UITextFieldTextDidChangeNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil]; \u7136\u540e\u5b9e\u73b0\u8fd9\u4e2a\u901a\u77e5\u65b9\u6cd5\u5373\u53ef\u3002<\/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":[20,17],"class_list":["post-591","post","type-post","status-publish","format-standard","hentry","category-project","tag-basics","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/591","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=591"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}