UITextField用法

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60, 180, 200, 35)]; android

    tf.tag = 101; api

    tf.delegate = self; // 設置代理 安全

    tf.textColor = [UIColor redColor]; less

    //提示用戶輸入的內容文本 字體

    tf.placeholder = @"用來提示用戶"; spa

    //自適應調整字體大小,默認爲NO 代理

    tf.adjustsFontSizeToFitWidth = YES; 事件

    //用戶編輯時是否Clean內容,默認是NO input

    tf.clearsOnBeginEditing = YES; string

    //清除按鈕的模式,默認不出現

    tf.clearButtonMode = UITextFieldViewModeWhileEditing;

    //    tf.background = [UIImage imageNamed:@"navigation"];

    tf.borderStyle = UITextBorderStyleRoundedRect;//顯示和android同樣的框

     [tf becomeFirstResponder];//響應鍵盤事件

 // 自定義clear按鈕

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

        view.backgroundColor = [UIColor yellowColor];

        tf.rightView = view;

        [view release];

        tf.rightViewMode = UITextFieldViewModeUnlessEditing;

 // 自定義系統鍵盤

    UIView *csView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];

    csView.backgroundColor = [UIColor yellowColor];

    tf.inputView = csView;

    [csView release];

 //系統鍵盤和自定義鍵盤共存

       tf.inputAccessoryView = csView1;

      //是否安全輸入,好比用戶名,密碼

      tf.secureTextEntry = YES;

     //修改鍵盤類型

      tf.keyboardType = UIKeyboardTypeNumberPad;

    //修改返回類型

      tf.returnKeyType = UIReturnKeyDone;

    //自動大寫類型

      tf.autocapitalizationType = UITextAutocapitalizationTypeNone;


    UITextField *tf = (UITextField *)[self.window viewWithTag:101];

    // 將鍵盤移除

    [tf resignFirstResponder];


代理方法:

#pragma mark - TextField Delegate

//將要開始輸入時調用,就是鍵盤要顯示時調用

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    NSLog(@"textFieldShouldBeginEditing");

    return YES; // [textField becomeFirstResponder]; 

}

//鍵盤已經顯示,作好編輯準備時調用

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    NSLog(@"textFieldDidBeginEditing");

}

//將要輸入結束時調用,就是鍵盤將要離開時調用

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    NSLog(@"textFieldShouldEndEditing");

    return YES; // [tf resignFirstResponder];

}

//鍵盤已經離開,結束編輯時調用,

- (void)textFieldDidEndEditing:(UITextField *)textField

{

    NSLog(@"textFieldDidEndEditing : %@", textField.text);

}

//文本改變監聽

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    NSLog(@"shouldChangeCharactersInRange : %@", string);

    return YES;

}

//清除文字按鈕點擊事件

- (BOOL)textFieldShouldClear:(UITextField *)textField

{

    NSLog(@"textFieldShouldClear");

    return YES;

}

//鍵盤上的return按鈕事件

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //隱藏輸入鍵盤

    [textField resignFirstResponder];

    return YES;

}

相關文章
相關標籤/搜索