UITextField
1.定義:它是一個輸入文本的控件
2.屬性
pswtextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
pswtextField.borderStyle = UITextBorderStyleRoundedRect; //設置邊框的類型
pswtextField.placeholder = @"輸入文本";// 提示的文字 當編輯時 消失
pswtextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; //設置彈出鍵盤的類型
pswtextField.keyboardAppearance = UIKeyboardAppearanceDark; //設置鍵盤樣式
pswtextField.secureTextEntry = YES;//設置成 不是明文顯示
pswtextField.returnKeyType = UIReturnKeyDone;// 設置return按鈕的類型
pswtextField.clearButtonMode = UITextFieldViewModeWhileEditing;// 設置清除按鈕的出現類型
UIView *left = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];
left.backgroundColor = [UIColor lightGrayColor];
pswtextField.leftView = left;// 把需要放到TextField的左邊或者右邊的視圖,賦值給 TextField的左邊或者右邊 還需要設置左邊或者右邊的視圖樣式
pswtextField.leftViewMode = UITextFieldViewModeAlways;// 左視圖是否一直存在
pswtextField.clearsOnBeginEditing = YES;
// 設置UIView的拐角
left.layer.cornerRadius = 4;
left.layer.masksToBounds = YES;
// 設置TextField的背景圖片
pswtextField.background = [UIImage imageNamed:@"234"];
// pswtextField.disabledBackground = [UIImage imageNamed:@"asdf"];
//// enabled 是否禁用控件 默認是YES 沒有禁用
pswtextField.enabled = YES;
代理
TextField的代理方法 讓別人幫忙做某件事 自己在本類實現不了功能 讓其他類 幫咱們實現 我們需要讓TextField 幫我們 獲得輸入完成後的字符串 如果要使用代理 需先添加代理的協議 在使用的地方 掛上代理 如果發現代理出問題 先檢查是否掛上代理
代理方法:
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// return YES;
//}
// 清空的時候調用
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"清空了");
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// 可以得到用戶輸入的字符 單獨字符
NSLog(@"%@",string);
return YES;
}
// 已經開始編輯的時候會觸發
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"已經開始編輯");
}
// 完成編輯的時候會觸發
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%@",textField.text);
if ([textField.text isEqualToString:@"xiaoming"]) {
NSLog(@"登錄成功");
}else{
NSLog(@"輸入賬號錯誤");
}
}
// 點擊return的時候調用
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)showAlertWithMessage:(NSString *)message
{
UIAlertView *tishi = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[tishi show];
}