{"id":559,"date":"2018-02-23T23:40:02","date_gmt":"2018-02-23T15:40:02","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/ios-%e6%96%87%e6%9c%ac%e5%ad%97%e6%ae%b5%ef%bc%88text-field%ef%bc%89\/"},"modified":"2018-02-23T23:40:02","modified_gmt":"2018-02-23T15:40:02","slug":"ios-%e6%96%87%e6%9c%ac%e5%ad%97%e6%ae%b5%ef%bc%88text-field%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=559","title":{"rendered":"iOS \u2013 \u6587\u672c\u5b57\u6bb5\uff08Text Field\uff09"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h2>\u6587\u672c\u5b57\u6bb5\u7684\u4f7f\u7528<\/h2>\n<p>\u6587\u672c\u5b57\u6bb5\u662f\u4e00\u4e2a\u7528\u6237\u754c\u9762\u5143\u7d20\uff0c\u901a\u8fc7\u5e94\u7528\u7a0b\u5e8f\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<p>\u4e00\u4e2aUITextfield\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"textfeild\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/textfeild.jpg\" width=\"251\" height=\"63\"><\/p>\n<p>\u91cd\u8981\u7684\u6587\u672c\u5b57\u6bb5\u7684\u5c5e\u6027<\/p>\n<ul>\n<li>\u5728\u6ca1\u6709\u4efb\u4f55\u7528\u6237\u8f93\u5165\u65f6\uff0c\u663e\u793a\u5360\u4f4d\u7b26<\/li>\n<li>\u6b63\u5e38\u6587\u672c<\/li>\n<li>\u81ea\u52a8\u66f4\u6b63\u578b<\/li>\n<li>\u952e\u76d8\u7c7b\u578b<\/li>\n<li>\u8fd4\u56de\u952e\u7c7b\u578b<\/li>\n<li>\u6e05\u9664\u6309\u94ae\u6a21\u5f0f<\/li>\n<li>\u5bf9\u9f50\u65b9\u5f0f<\/li>\n<li>\u59d4\u6258<\/li>\n<\/ul>\n<hr>\n<h2>\u66f4\u65b0xib\u4e2d\u7684\u5c5e\u6027<\/h2>\n<p>\u53ef\u4ee5\u5728Utility area\uff08\u5b9e\u7528\u533a\u57df\uff0c\u7a97\u53e3\u7684\u53f3\u4fa7\uff09\u66f4\u6539xib\u5728\u5c5e\u6027\u67e5\u770b\u5668\u4e2d\u7684\u6587\u672c\u5b57\u6bb5\u5c5e\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"UITextField_Attribute\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/UITextField_Attribute.jpg\" width=\"246\" height=\"480\" data-pinit=\"registered\"><\/p>\n<hr>\n<h2>\u6587\u672c\u5b57\u6bb5\u59d4\u6258<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53f3\u51fb\u00a0UIElement\u00a0\u754c\u9762\u751f\u6210\u5668\u4e2d\u8bbe\u7f6e\u59d4\u6258\u5e76\u5c06\u5b83\u8fde\u63a5\u5230\u6587\u4ef6\u7684\u6240\u6709\u8005\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"TextfieldDelegate\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/TextfieldDelegate.jpg\" width=\"560\" height=\"406\" data-pinit=\"registered\"><\/p>\n<p>\u4f7f\u7528\u59d4\u6258\u7684\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>1.\u8bbe\u7f6e\u59d4\u6258\u5982\u4e0a\u56fe\u6240\u793a<\/li>\n<li>2.\u6dfb\u52a0\u59d4\u6258\u5230\u60a8\u7684\u54cd\u5e94\u7c7b<\/li>\n<li>3.\u6267\u884c\u6587\u672c\u5b57\u6bb5\u4ee3\u8868\uff0c\u91cd\u8981\u7684\u6587\u672c\u5b57\u6bb5\u4ee3\u8868\u5982\u4e0b:<\/li>\n<\/ul>\n<pre>\n- (void)textFieldDidBeginEditing:(UITextField *)textField\n<\/pre>\n<pre>\n- (void)textFieldDidEndEditing:(UITextField *)textField\n<\/pre>\n<li>4.\u6b63\u5982\u5176\u540d\u79f0\u6240\u6697\u793a\uff0c\u4e0a\u8ff0\u4e24\u4e2a\u59d4\u6258\u5206\u522b\u53eb\u505a\u7f16\u8f91\u7684\u6587\u672c\u5b57\u6bb5\u548c\u7ed3\u675f\u7f16\u8f91<\/li>\n<li>5. \u5176\u4ed6\u7684\u59d4\u6258\u8bf7\u67e5\u770b UITextDelegate Protocol \u53c2\u8003\u624b\u518c\u3002<\/li>\n<p><\/p>\n<hr>\n<h2>\u5b9e\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u6211\u4eec\u4f7f\u7528\u7b80\u5355\u7684\u5b9e\u4f8b\u6765\u521b\u5efaUI\u5143\u7d20<\/p>\n<p>ViewController \u7c7b\u5c06\u91c7\u7528UITextFieldDelegate\uff0c\u4fee\u6539ViewController.h\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n<p>\u5c06\u65b9\u6cd5addTextField\u6dfb\u52a0\u5230\u6211\u4eec\u7684 ViewController.m \u6587\u4ef6<\/p>\n<p>\u7136\u540e\u5728 viewDidLoad \u65b9\u6cd5\u4e2d\u8c03\u7528\u6b64\u65b9\u6cd5<\/p>\n<p>\u5728ViewController.m\u4e2d\u66f4\u65b0viewDidLoad\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>#import \"ViewController.h\"\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \/\/The custom method to create our textfield is called\n    [self addTextField];\n    \/\/ Do any additional setup after loading the view, typically from a nib.\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n-(void)addTextField{\n   \/\/ This allocates a label \n   UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero];\n   \/\/This sets the label text\n   prefixLabel.text =@\"## \";\n   \/\/ This sets the font for the label\n   [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]];\n   \/\/ This fits the frame to size of the text\n   [prefixLabel sizeToFit];\n\n   \/\/ This allocates the textfield and sets its frame\n   UITextField *textField = [[UITextField  alloc] initWithFrame:\n   CGRectMake(20, 50, 280, 30)];\n\n   \/\/ This sets the border style of the text field \n   textField.borderStyle = UITextBorderStyleRoundedRect;\n   textField.contentVerticalAlignment =\n   UIControlContentVerticalAlignmentCenter;\n   [textField setFont:[UIFont boldSystemFontOfSize:12]];\n\n   \/\/Placeholder text is displayed when no text is typed\n   textField.placeholder = @\"Simple Text field\";\n\n   \/\/Prefix label is set as left view and the text starts after that\n   textField.leftView = prefixLabel;\n\n   \/\/It set when the left prefixLabel to be displayed\n   textField.leftViewMode = UITextFieldViewModeAlways;\n\n   \/\/ Adds the textField to the view.\n   [self.view addSubview:textField];\n\n   \/\/ sets the delegate to the current class\n   textField.delegate = self;\n}\n\n\/\/ pragma mark is used for easy access of code in Xcode\n#pragma mark - TextField Delegates\n\n\/\/ This method is called once we click inside the textField\n-(void)textFieldDidBeginEditing:(UITextField *)textField{\n   NSLog(@\"Text field did begin editing\");\n}\n\n\/\/ This method is called once we complete editing\n-(void)textFieldDidEndEditing:(UITextField *)textField{\n   NSLog(@\"Text field ended editing\");\n}\n\n\/\/ This method enables or disables the processing of return key\n-(BOOL) textFieldShouldReturn:(UITextField *)textField{    \n    [textField resignFirstResponder];\n    return YES;\n}\n\n- (void)viewDidUnload {\n   label = nil;\n   [super viewDidUnload];\n}\n\n@end<\/pre>\n<p>\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\u4f1a\u770b\u5230\u4e0b\u9762\u7684\u8f93\u51fa<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"TextfieldOutput\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/TextfieldOutput.jpg\" width=\"346\" height=\"693\" data-pinit=\"registered\"><\/p>\n<p>\u59d4\u6258\u8c03\u7528\u7684\u65b9\u6cd5\u57fa\u4e8e\u7528\u6237\u64cd\u4f5c\u3002\u8981\u77e5\u9053\u8c03\u7528\u59d4\u6258\u65f6\u8bf7\u53c2\u9605\u63a7\u5236\u53f0\u8f93\u51fa\u3002<\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u6587\u672c\u5b57\u6bb5\u7684\u4f7f\u7528 \u6587\u672c\u5b57\u6bb5\u662f\u4e00\u4e2a\u7528\u6237\u754c\u9762\u5143\u7d20\uff0c\u901a\u8fc7\u5e94\u7528\u7a0b\u5e8f\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002 \u4e00\u4e2aUIText&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=559\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-559","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=559"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}