好记性,不如烂笔头,记录一下。
方法一:通过attributedPlaceholder属性修改Placeholder颜色
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:textField.font }];
textField.attributedPlaceholder = attrString;
方法二:通过KVC修改Placeholder颜色
// "通过KVC修改占位文字的颜色"
[textField1 setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
方法三:通过重写UITextField的drawPlaceholderInRect:方法修改Placeholder颜色
1、自定义一个TextField继承自UITextField
2、重写drawPlaceholderInRect:方法
3、在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}];
}
参考链接:https://www.jianshu.com/p/3bd82283509b
网友评论