UILabel用于提示用户操作,它负责显示文本的信息
UITextField和UIView可用于接收用户输入,前者通常显示一行,后者可以输入很多数据;二者用法相似。
这里主要详细讲解UITextField的用法
1.UITextField基本属性设置
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createTextField]; } - (void)createTextField{ //1.创建textField并初始化 UITextField *textField = [[UITextField alloc]init]; textField.frame = CGRectMake(30, 30, 200, 50); /* 等同于(创建的同时初始化) UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 30, 200, 100)]; */ //2.设置边框样式(只有设置了才会有显示边框样式[如果只做了上面的1,显示的是一片空白]) textField.borderStyle = UITextBorderStyleRoundedRect;//圆角 /* typedef enum{ UITextBorderStyleNone,//无边框 UITextBorderStyleLine,//有黑色边框 UITextBorderStyleBezel,//有灰色边框和阴影 UITextBorderStyleRoundedRect//圆角 } UITextBorderStyle */ //3.设置边框颜色 textField.layer.borderColor = [UIColor greenColor].CGColor; textField.layer.borderWidth = 1; //4.设置输入框的背景颜色(如果使用了自定义的背景图片边框会被忽略掉 textField.backgroundColor = [[UIColor alloc]initWithRed:250/255.0 green:240/255.0 blue:230/255.0 alpha:1]; //5.设置背景(只有在UITextBorderStyleNone样式下,设置背景图才会生效,且图片大小小于textField的frame时,图片才会拉伸) textField.background = [UIImage imageNamed:@"1.png"]; //6.默认提示内容(用户开始输入的时候就自动消失) textField.placeholder = @"请输入密码"; //7.设置文字的属性 textField.textColor = [UIColor redColor];//设置输入的文字颜色 textField.textAlignment = NSTextAlignmentLeft;//居左(此为默认)(Right/Center:右/中间) /* 8.设置键盘的风格 textField.keyboardType = UIKeyboardTypeDefault;//默认键盘,支持所有字符 textField.keyboardType = UIKeyboardTypeNumberPad;//数字键盘 textField.keyboardType = UIKeyboardTypeDecimalPad;//数字键盘,有数字和小数点 textField.keyboardType = UIKeyboardTypePhonePad;//电话键盘 textField.keyboardType = UIKeyboardTypeNamePhonePad;//电话键盘,也支持输入人名 textField.keyboardType = UIKeyboardTypeEmailAddress;//电子邮件地址键盘 textField.keyboardType = UIKeyboardTypeTwitter;//优化的键盘,方便输入@、#字符 */ //9.输入框中有个❎ 可设置显示时间 textField.clearButtonMode = UITextFieldViewModeAlways; /* typedef enum{ UITextFieldViewModeNever, //从不出现 UITextFieldViewModeWhileEditing, //编辑时出现 UITextFieldViewModeUnlessEditing, //除了编辑外都出现 UITextFieldViewModeAlways //一直出现 } UITextFieldViewMode; */ //10.安全输入(每输入一个字符就变成一个点) textField.secureTextEntry = YES; [self.view addSubview:textField]; } @end