//1.設置背景 //tf.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.7]; //2.設置輸入框的樣式 /* UITextBorderStyleNone, 預設樣式,無樣式 UITextBorder...
//1.設置背景
//tf.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
//2.設置輸入框的樣式
/*
UITextBorderStyleNone, 預設樣式,無樣式
UITextBorderStyleLine, 矩形線條樣式
UITextBorderStyleBezel, 刀鋒線條樣式
UITextBorderStyleRoundedRect 圓角樣式
*/
tf.borderStyle = UITextBorderStyleRoundedRect;
//獲取圖片
UIImage *image = [UIImage imageNamed:@"btn_bg"];
//用區域的方式拉伸圖片
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2)];
//3.設置背景圖片
tf.background = [UIImage imageNamed:@"btn_bg"];
//4.設置文字顏色
tf.textColor = [UIColor grayColor];
//5.設置文字的字體和大小
tf.font = [UIFont systemFontOfSize:20];
//6.設置對齊方式
//tf.textAlignment = NSTextAlignmentCenter;
//7.文字自適應輸入框的寬度
tf.adjustsFontSizeToFitWidth = YES;
//8.設置最小文字自適應度
tf.minimumFontSize = 5;
//9.設置textField左邊的圖片,見圖
UIImageView *leftImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 25 )];
leftImageV.image = [UIImage imageNamed:@"username_icon"];
tf.leftView = leftImageV;
/*
UITextFieldViewModeNever, 不顯示
UITextFieldViewModeWhileEditing, 當編輯狀態時顯示
UITextFieldViewModeUnlessEditing,不是編輯狀態顯示
UITextFieldViewModeAlways 始終顯示
*/
tf.leftViewMode = UITextFieldViewModeAlways ;
//設置右邊圖片,道理同上面的設置左邊圖片
//tf.rightView
//tf.rightViewMode
//10.設置占位符,即一開始輸入框裡面的文字,當我們輸入值的時候就沒了
//tf.placeholder = @"請輸入用戶名";
//修改占位符的顏色和文字大小
tf.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"請輸入用戶名" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blueColor]}];
//11.顯示右邊的清空按鈕,見圖
/*
UITextFieldViewModeNever, 都不顯示
UITextFieldViewModeWhileEditing, 當編輯時顯示
UITextFieldViewModeUnlessEditing, 不編輯時顯示
UITextFieldViewModeAlways 一直顯示
*/
tf.clearButtonMode = UITextFieldViewModeWhileEditing
//12.安全輸入,適用於密碼輸入
tf.secureTextEntry = NO;
//13.當輸入狀態時自動清空
tf.clearsOnBeginEditing = YES;
//14.不允許編輯,很少使用這個方法
//tf.enabled = NO;
//15.設置鍵盤樣式
//tf.keyboardType = UIKeyboardTypeNumberPad;
//16.設置返回按鈕的樣式,有好多種樣式,如圖,這裡就不一一演示了,大家可以自己實現看下
tf.returnKeyType = UIReturnKeyJoin;
//17.是否糾錯,比如當你想要輸入“Hello”時,打字打成“Hollo”,系統會自動幫你糾錯
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
*/
tf.autocorrectionType = UITextAutocorrectionTypeYes;