UITextField的使用詳解

UITextField控件是開發中,使用頻率比較高的控件了,那麼有必要總結一下。


一、UITextField手動編寫控件


UITextField  *txtAccount = [[UITextField allocinitWithFrame:CGRectMake(1010,30030)];

    

    // 設置委託

    [txtAccount setDelegate:self];

    // 設置佔位符

    [txtAccount setPlaceholder:@"賬號"];

    // 設置文本對齊

    [txtAccount setTextAlignment:NSTextAlignmentLeft];

    // 設置樣式

    [txtAccount setBorderStyle:UITextBorderStyleRoundedRect];

    // 加入view

    [self.view addSubview: txtAccount];

    [txtAccount release];


二、UITextFieldDelegate委託


// 設置輸入框,是否可以被修改

// NO-將無法修改,不出現鍵盤

// YES-可以修改,默認值 

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

    return YES;


}


// 當點擊鍵盤的返回鍵(右下角)時,執行該方法。

// 一般用來隱藏鍵盤

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

    if (txtAccount == textField) {

[txtAccount resignFirstResponder];

}

return YES;

}


// 當輸入框獲得焦點時,執行該方法。 

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

    NSLog(@"textFieldDidBeginEditing");


}


// 指定是否允許文本字段結束編輯,允許的話,文本字段會失去first responder 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    return YES;


}


// 文本框失去first responder 時,執行 

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

     NSLog(@"textFieldDidEndEditing");


}


 

// 指明是否允許根據用戶請求清除內容

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

    NSLog(@"textFieldDidEndEditing");

    return YES;

}


// 文本框的文本,是否能被修改 

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

    return YES;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章