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;
新聞熱點
疑難解答