iOS学习(UI)知识点整理
1 //创建一个UItextField实例 2 UITextField *textField = [[UITextField alloc] init]; 3 textField.frame = CGRectMake(10, 40, self.view.frame.size.width - 20, 40); 4 textField.backgroundColor = [UIColor lightGrayColor]; 5 //设置textFiled中的文字 6 textField.text = @"用户名"; 7 //设置textFiled的字体 8 textField.font = [UIFont systemFontOfSize:20]; 9 //设置textFiled的文字颜色10 textField.textColor = [UIColor purpleColor];11 [self.view addSubview: textField];
1 //设置textFiled的文本左对齐2 textField.textAlignment = NSTextAlignmentLeft;
1 //设置textFiled样式为无边框样式2 textField.borderStyle = UITextBorderStyleNone;3 //borderStyle 有以下几种类型4 //1、UITextBorderStyleNone,5 //2、UITextBorderStyleLine,6 //3、UITextBorderStyleBezel,7 //4、UITextBorderStyleRoundedRect
1 textField.layer.cornerRadius = 4.0f;
1 textField.layer.borderWidth = 1;
1 textField.layer.borderColor = [UIColor darkGrayColor].CGColor;
1 UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];2 textField.background = image;
方法一:placeholder
1 textField.placeholder = @"用户名";
方法二: NSMutableAttributedString
1 NSMutableAttributedString *muAttStr = [[NSMutableAttributedString alloc] initWithString:@"用户名"];2 [muAttStr addAttribute:NSForegroundColorAttributeName value:3 [UIColor blueColor] range:NSMakeRange(0, muAttStr.length)];4 textField.attributedPlaceholder = muAttStr;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
typedef enum { UITextFieldViewModeNever, //从不出现 UITextFieldViewModeWhileEditing, //编辑时出现 UITextFieldViewModeUnlessEditing, //除了编辑外都出现 UITextFieldViewModeAlways //一直出现} UITextFieldViewMode;
1 UIView *leftView = [[UIView alloc] init];2 leftView.backgroundColor = [UIColor clearColor];3 leftView.frame = CGRectMake(0, 0, 50, 40);4 textField.leftView = iconImgView;5 //设置文本框左边视图的出现方式6 textField.leftViewMode = UITextFieldViewModeAlways;
1 textField.returnKeyType = UIReturnKeyDone;
1 //设置文本框不可编辑2 textField.userInteractionEnabled=NO;
1 [textField becomeFirstResponder];2 //注意:当设置一个控件为第一响应者之后,再设置其他为第一响应者无效 第一响应者有且只能有一个
1 [textField resignFirstResponder];
1 textField .secureTextEntry = YES;
1 //设置数字键盘 2 textField.keyboardType = UIKeyboardTypeNumberPad; 3 4 //keyboardType 有: 5 typedef enum { 6 UIKeyboardTypeDefault, // 默认键盘,支持所有字符 7 UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘 8 UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符 9 UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符10 UIKeyboardTypeNumberPad, //数字键盘11 UIKeyboardTypePhonePad, // 电话键盘12 UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名13 UIKeyboardTypeEmailAddress, //用于输入电子 邮件地址的键盘 14 UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点15 UIKeyboardTypeTwitter, //优化的键盘,方便输入@、#字符16 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,17 } UIKeyboardType;
1 textField.adjustsFontSizeToFitWidth = YES;2 //设置自动缩小显示的最小字体大小3 textField.minimumFontSize = 20;
//深灰 石墨色键盘2 textField.keyboardAppearance=UIKeyboardAppearanceAlert;3 typedef enum {4 UIKeyboardAppearanceDefault, //默认外观,浅灰色5 UIKeyboardAppearanceAlert, // 深灰 石墨色 6 } UIReturnKeyType;
1 //内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment2 textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
1 textField.font = [UIFont fontWithName:@"Arial" size:20.0f];
textField.delegate = self;//文本框设置代理时当前类先必须遵守 UITextFieldDelegate 协议然后实现它的协议方法//它的协议方法有: //返回一个BOOL值,是否允许文本框开始编辑 1、- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//开始编辑时触发,文本框将成为第一响应者(first responder ) 2、- (void)textFieldDidBeginEditing:(UITextField *)textField;//返回BOOL值,是否允许文本框结束编辑,当编辑结束,当前文本框会放弃第一响应者身份 3、- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;//当文本框结束编辑时触发此方法4、- (void)textFieldDidEndEditing:(UITextField *)textField;//当文本框内容改变时触发此方法,可在此方法中对文本内容做非法字符筛选或限制输入5、- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; //当点击文本框右边清除按钮时触发此方法6、- (BOOL)textFieldShouldClear:(UITextField *)textField; //当点击键盘右下角按钮Return 按钮时触发此方法 7、- (BOOL)textFieldShouldReturn:(UITextField *)textField;
新闻热点
疑难解答