1 // 2 // ViewController.m 3 // UITextField详解 4 // 5 // Created by 大欢 on 16/1/21. 6 // Copyright © 2016年 bjsxt. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (nonatomic, strong) UITextField * textField; 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 20 UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 60)]; 21 /* 22 23 设置边框样式 24 typedef NS_ENUM(NSInteger, UITextBorderStyle) { 25 UITextBorderStyleNone, // 什么都没有(默认) 26 UITextBorderStyleLine, // 周围加黑色线条 27 UITextBorderStyleBezel, // 周围加灰色线条,上、左加阴影 28 UITextBorderStyleRoundedRect // 带圆角四周加灰色线条 29 }; 30 textFeld.borderStyle = UITextBorderStyleRoundedRect; 31 32 */ 33 textField.borderStyle = UITextBorderStyleRoundedRect; 34 // textField.text = @"请输入密码"; 35 //设置提示文字 36 textField.placeholder = @"请输入密码"; 37 //设置输入文字的颜色 38 textField.textColor = [UIColor redColor]; 39 //开始编辑是否清除文本 40 // textField.clearsOnBeginEditing = YES; 41 // textField.textAlignment = NSTextAlignmentCenter; 42 //设置字体 43 textField.font = [UIFont systemFontOfSize:50]; 44 //字体适应宽度 45 // textField.adjustsFontSizeToFitWidth = YES; 46 //设置最小字体 47 // textField.minimumFontSize = 1; 48 //设置删除按钮的出现时间 49 // textField.clearButtonMode = UITextFieldViewModeWhileEditing; 50 51 //设置textField的左视图 52 UIView * small = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; 53 small.backgroundColor = [UIColor grayColor]; 54 55 textField.leftView = small; 56 textField.leftViewMode = UITextFieldViewModeAlways; 57 58 //设置安全密码 59 // textField.secureTextEntry = YES; 60 61 /* 62 63 设置键盘的样式 64 typedef NS_ENUM(NSInteger, UIKeyboardType) { 65 UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable,支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 66 UIKeyboardTypeURL,URL , 键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, 数字键盘 67 UIKeyboardTypePhonePad, 电话键盘 68 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名 69 UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 70 UIKeyboardTypeDecimalPad 数字键盘 有数字和小数点 UIKeyboardTypeTwitter 优化的键盘,方便输入@、#字符 71 }; 72 73 74 */ 75 76 textField.keyboardType = UIKeyboardTypeEmailAddress; 77 78 /* 79 80 return键变成什么键 81 typedef NS_ENUM(NSInteger, UIReturnKeyType) { 82 UIReturnKeyDefault, //默认 灰色按钮,标有Return 83 UIReturnKeyGo, //标有Go的蓝色按钮 84 UIReturnKeyGoogle, //标有Google的蓝色按钮,用语搜索 85 UIReturnKeyJoin, //标有Join的蓝色按钮 86 UIReturnKeyNext, //标有Next的蓝色按钮 87 UIReturnKeyRoute, //标有Route的蓝色按钮 88 UIReturnKeySearch, //标有Search的蓝色按钮 89 UIReturnKeySend, //标有Send的蓝色按钮 90 UIReturnKeyYahoo, //标有Yahoo的蓝色按钮 91 UIReturnKeyDone, //标有Done的蓝色按钮 92 UIReturnKeyEmergencyCall, //紧急呼叫按钮 93 }; 94 95 */ 96 97 textField.returnKeyType = UIReturnKeyGo; 98 99 /* 100 101 输入字母大小写 102 typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) { 103 UITextAutocapitalizationTypeNone, // 不自动大写 (默认) 104 UITextAutocapitalizationTypeWords, // 单词首字母大写 105 UITextAutocapitalizationTypeSentences, // 句子的首字母大写 106 UITextAutocapitalizationTypeAllCharacters, // 所有字母都大写 107 }; 108 textFeld.autocapitalizationType = UITextAutocapitalizationTypeNone; 109 110 111 */ 112 textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 113 114 self.textField = textField; 115 116 [self.view addSubview:textField]; 117 118 119 } 120 121 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 122 123 // 取消第一响应 124 // [self.textField resignFirstResponder]; 125 126 //结束编辑 127 [self.textField endEditing:YES]; 128 129 } 130 131 @end
时间: 2024-10-29 03:57:56