基本属性
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 取消第一响应效果一样,好用!
简单定义
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 :默认的提示文字是与设置输入文字字体一致,颜色深灰色,
attributedPlaceholder:自定义富文本,输入内容也可以使用attributedText
简单举例,具体看Foundation 中的 NSAttributedString 使用。
self.testField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"test"
attributes:@{
NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont systemFontOfSize:22]
}];
代理
没啥好说的。
- (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"]) //按会车可以改变
{
return YES;
}
// 控制 长度
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
if (self.testField == textField) {
if ([toBeString length] > 20) { //如果输入框内容大于20则弹出警告
textField.text = [toBeString substringToIndex:20];
NSLog(@"长度过长");
return NO;
}
}
// 控制 内容
cs = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890\n"]invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
BOOL canChange = [string isEqualToString:filtered];
return canChange;
}
通知
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
1 举例,输入变化监听
- (void)addNotification {
NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testFieldChange:) name:UITextFieldTextDidChangeNotification object:self.testField];
}
- (void)testFieldChange:(NSNotification *)notification {
NSLog(@"%@",self.testField.text);
}
2 实现上述功能还可以使用 UIControl 类的方法。
[self.testField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
3 再其他就是 KVO 方式了。
重绘
修改 输入框的一些位置,等需要重绘,例如输入内容与框下边对齐,而上边空的多,举例:
- (CGRect)editingRectForBounds:(CGRect)bounds {
// 输入内容 向下偏移 8 px
return CGRectOffset(bounds, 0, - 8);
}
- (CGRect)textRectForBounds:(CGRect)bounds {
// 显示内容 向上偏移 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;