天天看點

iOS開發-UI (五)UITextField

UITextField使用

   1.建立方式

 例:

  UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];

   2.常用方法和屬性

     1)邊框樣式

       @property(nonatomic)  UITextBorderStyle   borderStyle; 

UITextBorderStyleNone                       沒有邊框,背景預設為透明

UITextBorderStyleLine                       線框,背景預設為透明

UITextBorderStyleBezel bezel           風格邊框,背景預設為透明

UITextBorderStyleRoundedRect         圓角邊框,背景預設為白色

textField.borderStyle = UITextBorderStyleBezel;

     2)提示文字: placeholder 

textField.placeholder = @"請輸入銀行卡密碼";

     3)鍵盤類型: keyboardType

textField.keyboardType = UIKeyboardTypeNumberPad;

     4)鍵盤樣式: keyboardAppearance

textField.keyboardAppearance = UIKeyboardAppearanceLight;

     5)密文輸入: secureTextEntry 

textField.secureTextEntry = YES;

     6)再次編輯是否清空: clearsOnBeginEditing

textField.clearsOnBeginEditing = YES;

     7)文本橫向對齊方式: textAlignment

textField.textAlignment = NSTextAlignmentRight;

     8)文本滾動: adjustsFontSizeToFitWidth 

搭配 minimumFontSize一起使用

//回收鍵盤

    [self.view endEditing: YES];

     9)return鍵類型:returnKeyType

@property(nonatomic) UIReturnKeyType returnKeyType; 

UIReturnKeyDefault,

    UIReturnKeyGo,

    UIReturnKeyGoogle,

    UIReturnKeyJoin,

    UIReturnKeyNext,

    UIReturnKeyRoute,

    UIReturnKeySearch,

    UIReturnKeySend,

    UIReturnKeyYahoo,

    UIReturnKeyDone,

    UIReturnKeyEmergencyCall,

     10)清理按鈕模式:clearButtonMode

@property(nonatomic)        UITextFieldViewMode  clearButtonMode;

UITextFieldViewModeNever,

    UITextFieldViewModeWhileEditing,

    UITextFieldViewModeUnlessEditing,

    UITextFieldViewModeAlways

   3.UITextFieldDelegate 協定

     1)是否可以進入編輯模式

     - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

//傳回NO,無法進入編輯狀态

    return YES;

     2)文本框已經進入編輯模式

     -(void)textFieldDidBeginEditing:(UITextField *)textField;

     3)文本框是否可以結束編輯模式

     -(BOOL)textFieldShowEndEditing:(UITextField *)textField;

//傳回NO,無法結束編輯狀态

    return YES;

     4)文本框已結束編輯模式

     -(void)textFieldDidEndEditing:(UITextField *)textField;

     5)是否可以點選clear按鈕

     -(BOOL)textFieldShouldClear:(UITextField *)textField;

//傳回NO,點選clear按鈕無響應

    return YES;

     6)是否可以點選return按鈕

     -(BOOL)textFieldShouldReturn:(UITextField *)textField;

    //移除第一響應者

    [textField resignFirstResponder];   

    return YES;

     7)允許修改内容

- (BOOL)textField:(UITextField *)textField 

shouldChangeCharactersInRange:(NSRange)range 

    replacementString:(NSString *)string;

  例如:

if (textField.text.length >= 6) {      
        if ([string isEqualToString:@""]) {

            return YES;
        }
        return NO;

    }
    return YES;
 }      

轉載于:https://www.cnblogs.com/fcug/p/6308630.html