1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, 335, 34)]; //文本框的边框设定 个人觉得最后一种最好看 textField.borderStyle = UITextBorderStyleLine; UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect //文本框内默认文字 textField.text = @“11”; //文本框内提示文字 textField.placeholder = @"请输入文本"; //设置输入框内容的字体样式和大小 textField.font = [UIFont fontWithName:@"Arial" size:20]; //设置字体颜色 textField.textColor = [UIColor redColor]; //设置输入框的背景颜色,如果使用了自定义的背景图片边框会被忽略掉 textField.backgroundColor = [UIColor whiteColor]; //设置背景 textField.background = [UIImage imageNamed:@"dd.png"]; //设置disabled状态的背景 textField.disabledBackground = [UIImage imageNamed:@"cc.png"]; //设置清除按钮模式 textField.clearButtonMode = UITextFieldViewModeAlways; UITextFieldViewModeNever, 从不出现 UITextFieldViewModeWhileEditing, 编辑时出现 UITextFieldViewModeUnlessEditing, 除了编辑外都出现 UITextFieldViewModeAlways 一直出现 //是否纠错 textField.autocorrectionType = UITextAutocorrectionTypeNo; UITextAutocorrectionTypeDefault, 默认 UITextAutocorrectionTypeNo, 不自动纠错 UITextAutocorrectionTypeYes, 自动纠错 //再次编辑就清空 textField.clearsOnBeginEditing = YES; //内容对齐方式 textField.textAlignment = NSTextAlignmentLeft; // center/right //内容的垂直对齐方式 textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 textFied.adjustsFontSizeToFitWidth = YES; //设置自动缩小显示的最小字体大小 textField.minimumFontSize = 20; //设置键盘的样式 textField.keyboardType = UIKeyboardTypeNumberPad; UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, 数字键盘 UIKeyboardTypePhonePad, 电话键盘 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名 UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 //首字母是否大写 text.autocapitalizationType = UITextAutocapitalizationTypeNone; UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写 //return键变成什么键 text.returnKeyType =UIReturnKeyDone; UIReturnKeyDefault, 默认 灰色按钮,标有Return UIReturnKeyGo, 标有Go的蓝色按钮 UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索 UIReturnKeyJoin,标有Join的蓝色按钮 UIReturnKeyNext,标有Next的蓝色按钮 UIReturnKeyRoute,标有Route的蓝色按钮 UIReturnKeySearch,标有Search的蓝色按钮 UIReturnKeySend,标有Send的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyYahoo,标有Yahoo的蓝色按钮 UIReturnKeyEmergencyCall, 紧急呼叫按钮 //键盘外观 text.keyboardAppearance=UIKeyboardAppearanceDefault UIKeyboardAppearanceDefault, 默认外观,浅灰色 UIKeyboardAppearanceAlert, 深灰 石墨色 //密码隐藏-安全输入模式 textField.secureTextEntry = NO;//默认NO,得手动开启 //文本自适应 [textField sizeToFit]; //回收键盘 [textField resignFirstResponder];
|