基本屬性
self.testField.font = [UIFont systemFontOfSize:12];
self.testField.textColor = [UIColor redColor];
self.testField.borderStyle = UITextBorderStyleNone;
self.testField.textAlignment = NSTextAlignmentCenter;
self.testField.backgroundColor = [UIColor yellowColor];
self.testField.adjustsFontSizeToFitWidth = YES;
self.testField.minimumFontSize = 8.;
[self.view endEditing:YES];// 取消編輯铣耘,與Field 取消第一響應(yīng)效果一樣昵仅,好用劫扒!
簡(jiǎn)單定義
self.testField.background = [UIImage imageNamed:@""];
self.testField.disabledBackground = [UIImage imageNamed:@""];
self.testField.clearsOnBeginEditing = YES;
self.testField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.testField.leftViewMode = UITextFieldViewModeAlways;
self.testField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
// rightView 同理
提示文字
placeholder :默認(rèn)的提示文字是與設(shè)置輸入文字字體一致诫睬,顏色深灰色巫延,
attributedPlaceholder:自定義富文本励两,輸入內(nèi)容也可以使用attributedText
簡(jiǎn)單舉例黎茎,具體看Foundation 中的 NSAttributedString 使用。
self.testField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"test"
attributes:@{
NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont systemFontOfSize:22]
}];
代理
沒(méi)啥好說(shuō)的当悔。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
- 比較特殊的 代理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSCharacterSet *cs;
// return YES 表示可以輸入傅瞻。
if ([string isEqualToString:@"\n"]) //按會(huì)車(chē)可以改變
{
return YES;
}
// 控制 長(zhǎng)度
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到輸入框的內(nèi)容
if (self.testField == textField) {
if ([toBeString length] > 20) { //如果輸入框內(nèi)容大于20則彈出警告
textField.text = [toBeString substringToIndex:20];
NSLog(@"長(zhǎng)度過(guò)長(zhǎng)");
return NO;
}
}
// 控制 內(nèi)容
cs = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890\n"]invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分離出數(shù)組,數(shù)組按@""分離出字符串
BOOL canChange = [string isEqualToString:filtered];
return canChange;
}
通知
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
1 舉例踢代,輸入變化監(jiān)聽(tīng)
- (void)addNotification {
NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testFieldChange:) name:UITextFieldTextDidChangeNotification object:self.testField];
}
- (void)testFieldChange:(NSNotification *)notification {
NSLog(@"%@",self.testField.text);
}
2 實(shí)現(xiàn)上述功能還可以使用 UIControl 類(lèi)的方法。
[self.testField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
3 再其他就是 KVO 方式了嗅骄。
重繪
修改 輸入框的一些位置胳挎,等需要重繪,例如輸入內(nèi)容與框下邊對(duì)齊溺森,而上邊空的多慕爬,舉例:
- (CGRect)editingRectForBounds:(CGRect)bounds {
// 輸入內(nèi)容 向下偏移 8 px
return CGRectOffset(bounds, 0, - 8);
}
- (CGRect)textRectForBounds:(CGRect)bounds {
// 顯示內(nèi)容 向上偏移 8 px
return CGRectOffset(bounds, 0, 8);
}
其他 更多重繪,更多驚喜屏积。
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;