- 最近项目时发现当在10以上设备上设置 describeField.borderStyle = UITextBorderStyleNone;
输入结束时候文字下移;在10以下设备却没有这种情况出现.
当出现该情况时,可设置textField的leftView
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
field.leftView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1, 21)];
field.leftViewMode = UITextFieldViewModeAlways;
2.1 方法一:通过attributedPlaceholder属性修改Placeholder颜色
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:
@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:textField.font }];
textField.attributedPlaceholder = attrString;
2.2 方法二:通过KVC修改Placeholder颜色
[textField1 setValue:[UIColor greenColor]forKeyPath:@"_placeholderLabel.textColor"];
2.3方法三:通过重写UITextField的drawPlaceholderInRect:方法修改Placeholder颜色
/ 重写此方法
-(void)drawPlaceholderInRect:(CGRect)rect {
// 计算占位文字的 Size
CGSize placeholderSize = [self.placeholder sizeWithAttributes: @{NSFontAttributeName : self.font}];
[self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height)/2, rect.size.width, rect.size.height) withAttributes:
@{NSForegroundColorAttributeName : [UIColor blueColor],
NSFontAttributeName : self.font}];
}
网友评论