UI基础 - UITextField
■ 简言
1. UITextField 是 iOS 开发中常用的控件之一,用于在应用程序中接收用户的文本输入。它可以放置在视图层次结构中的任何位置,并通过键盘输入文本
2. 它的核⼼功能主要包含三个方⾯
输入控制:密码模式、键盘样式、自定义键盘等
外观配置:边框样式、清除按钮、辅助视图等
文本显示
■ 使用方式
1 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 2*50, 150)]; 2 textField.backgroundColor = [UIColor cyanColor]; // 背景颜色 3 textField.borderStyle = UITextBorderStyleRoundedRect;// 边框样式 4 5 textField.text = @"Bra and Hip"; // 文本 6 textField.placeholder = @"please input"; // 占位符 7 textField.textAlignment = NSTextAlignmentCenter;// 文本居中 8 9 textField.clearsOnBeginEditing = YES; // 是否开始输入时清空内容 10 textField.clearButtonMode = UITextFieldViewModeWhileEditing; // 编辑时清除文本 11 textField.font = [UIFont systemFontOfSize:12.5];// 字号 12 textField.textColor = [UIColor purpleColor]; // 字体的颜色 13 14 // textField.secureTextEntry = YES; // 密文输入 15 // textField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;// 垂直对齐方式 16 17 [self.view addSubview:textField]; 18 19 //---------------- 键盘 ----------------- 20 textField.keyboardType = UIKeyboardTypeNumberPad; // 弹出键盘样式 21 textField.keyboardAppearance = UIKeyboardAppearanceDark;// 弹出键盘外观 22 // textField.returnKeyType = UIReturnKeySearch; // 键盘回车键类型 23 24 // inputView:键盘视图 25 UIView *myInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]; 26 myInputView.backgroundColor = [UIColor blueColor]; 27 textField.inputView = myInputView;// 替换系统键盘 28 29 // inputAccessoryView:键盘上方的辅助视图 30 UIView *upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)]; 31 upView.backgroundColor = [UIColor cyanColor]; 32 textField.inputAccessoryView = upView; 33 34 // leftView/rightView:输入框左/右视图 35 UIImageView *imv = [[UIImageView alloc] initWithImage:nil]; 36 UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 100)];// 高度必须大于等于 textField 的高度 37 iv.backgroundColor = [UIColor yellowColor]; 38 imv.center = iv.center; 39 [iv addSubview:imv]; 40 textField.leftView = iv; 41 textField.leftViewMode = UITextFieldViewModeWhileEditing;// 只在编辑时显示
运行效果:初始化 | 点击编辑
|