iOS學(xué)習(xí)(UI)知識點(diǎn)整理
1 //創(chuàng)建一個UItextField實(shí)例 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 //設(shè)置textFiled中的文字 6 textField.text = @"用戶名"; 7 //設(shè)置textFiled的字體 8 textField.font = [UIFont systemFontOfSize:20]; 9 //設(shè)置textFiled的文字顏色10 textField.textColor = [UIColor purpleColor];11 [self.view addSubview: textField];
1 //設(shè)置textFiled的文本左對齊2 textField.textAlignment = NSTextAlignmentLeft;
1 //設(shè)置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, //從不出現(xiàn) UITextFieldViewModeWhileEditing, //編輯時出現(xiàn) UITextFieldViewModeUnlessEditing, //除了編輯外都出現(xiàn) UITextFieldViewModeAlways //一直出現(xiàn)} 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 //設(shè)置文本框左邊視圖的出現(xiàn)方式6 textField.leftViewMode = UITextFieldViewModeAlways;
1 textField.returnKeyType = UIReturnKeyDone;
1 //設(shè)置文本框不可編輯2 textField.userInteractionEnabled=NO;
1 [textField becomeFirstResponder];2 //注意:當(dāng)設(shè)置一個控件為第一響應(yīng)者之后,再設(shè)置其他為第一響應(yīng)者無效 第一響應(yīng)者有且只能有一個
1 [textField resignFirstResponder];
1 textField .secureTextEntry = YES;
1 //設(shè)置數(shù)字鍵盤 2 textField.keyboardType = UIKeyboardTypeNumberPad; 3 4 //keyboardType 有: 5 typedef enum { 6 UIKeyboardTypeDefault, // 默認(rèn)鍵盤,支持所有字符 7 UIKeyboardTypeASCIICapable, //支持ASCII的默認(rèn)鍵盤 8 UIKeyboardTypeNumbersAndPunctuation, //標(biāo)準(zhǔn)電話鍵盤,支持+*#字符 9 UIKeyboardTypeURL, //URL鍵盤,支持.com按鈕 只支持URL字符10 UIKeyboardTypeNumberPad, //數(shù)字鍵盤11 UIKeyboardTypePhonePad, // 電話鍵盤12 UIKeyboardTypeNamePhonePad, //電話鍵盤,也支持輸入人名13 UIKeyboardTypeEmailAddress, //用于輸入電子 郵件地址的鍵盤 14 UIKeyboardTypeDecimalPad, //數(shù)字鍵盤 有數(shù)字和小數(shù)點(diǎn)15 UIKeyboardTypeTwitter, //優(yōu)化的鍵盤,方便輸入@、#字符16 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,17 } UIKeyboardType;
1 textField.adjustsFontSizeToFitWidth = YES;2 //設(shè)置自動縮小顯示的最小字體大小3 textField.minimumFontSize = 20;
//深灰 石墨色鍵盤2 textField.keyboardAppearance=UIKeyboardAppearanceAlert;3 typedef enum {4 UIKeyboardAppearanceDefault, //默認(rèn)外觀,淺灰色5 UIKeyboardAppearanceAlert, // 深灰 石墨色 6 } UIReturnKeyType;
1 //內(nèi)容的垂直對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment2 textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
1 textField.font = [UIFont fontWithName:@"Arial" size:20.0f];
textField.delegate = self;//文本框設(shè)置代理時當(dāng)前類先必須遵守 UITextFieldDelegate 協(xié)議然后實(shí)現(xiàn)它的協(xié)議方法//它的協(xié)議方法有: //返回一個BOOL值,是否允許文本框開始編輯 1、- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//開始編輯時觸發(fā),文本框?qū)⒊蔀榈谝豁憫?yīng)者(first responder ) 2、- (void)textFieldDidBeginEditing:(UITextField *)textField;//返回BOOL值,是否允許文本框結(jié)束編輯,當(dāng)編輯結(jié)束,當(dāng)前文本框會放棄第一響應(yīng)者身份 3、- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;//當(dāng)文本框結(jié)束編輯時觸發(fā)此方法4、- (void)textFieldDidEndEditing:(UITextField *)textField;//當(dāng)文本框內(nèi)容改變時觸發(fā)此方法,可在此方法中對文本內(nèi)容做非法字符篩選或限制輸入5、- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; //當(dāng)點(diǎn)擊文本框右邊清除按鈕時觸發(fā)此方法6、- (BOOL)textFieldShouldClear:(UITextField *)textField; //當(dāng)點(diǎn)擊鍵盤右下角按鈕Return 按鈕時觸發(fā)此方法 7、- (BOOL)textFieldShouldReturn:(UITextField *)textField;
新聞熱點(diǎn)
疑難解答