iOS开发 UITextField Category 设置placeHolder字体以及颜色
方法一:
新建一category
.h文件
#import
NS_ASSUME_NONNULL_BEGIN
@interface UITextField (ZKChangePlacholder)
- (void)setPlaceholderFont:(UIFont *)font;
- (void)setPlaceholderTextColor:(UIColor *)color;
- (void)setPlaceholderFont:(UIFont *)font textColor:(UIColor *)color;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "UITextField+ZKChangePlacholder.h"
@implementation UITextField (ZKChangePlacholder)
- (void)setPlaceholderFont:(UIFont *)font{
[self setPlaceholderFont:font textColor:nil];
}
- (void)setPlaceholderTextColor:(UIColor *)color{
[self setPlaceholderFont:nil textColor:color];
}
- (void)setPlaceholderFont:(UIFont *)font textColor:(UIColor *)color{
if (!self.placeholder || [[self.placeholder stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
return;
}
NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:self.placeholder];
if (font) {
[mutAttStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.placeholder.length)];
}
if (color) {
[mutAttStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, self.placeholder.length)];
}
[self setAttributedPlaceholder:mutAttStr];
}
@end
使用
[textField setPlaceholderFont:[UIFont xxxxxx]textColor:[UIColor xxxxxx]];
方法二:
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(100, 100,200, 40);
textField.borderStyle = UITextBorderStyleRoundedRect; // 边框类型
textField.font = [UIFont systemFontOfSize:14];
// 就下面这两行是重点
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:textField.font,
NSBackgroundColorAttributeName:[UIColor yellowColor]
}];
textField.attributedPlaceholder =attrString;
[self.view addSubview:textField];
方法三:
自定义一个 XXTextField 继承自 UITextField;
重写自定义 XXTextField 的 drawPlaceholderInRect: 方法;
在 drawPlaceholderInRect 方法中设置 placeholder 的属性。
- (void)drawPlaceholderInRect:(CGRect)rect {
// 计算占位文字的 Size
NSDictionary *attributes = @{
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:20]
};
CGSize placeholderSize = [self.placeholder sizeWithAttributes:attributes];
[self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height)/2, rect.size.width, rect.size.height) withAttributes: attributes];
}
方法四:
利用 Runtime,找到并修改 UITextfield 的私有属性
实现步骤:
通过获取类的属性列表和成员变量列表的方法打印 UITextfield 所有属性和成员变量;
找到私有的成员变量 _placeholderLabel;
利用 KVC 对 _placeholderLabel 进行修改。
// 打印 UITextfield 的所有属性和成员变量
- (void)printUITextFieldList {
unsigned int count;
Ivar *ivarList = class_copyIvarList([UITextField class], &count);
for (unsigned int i = 0; i < count; i++) {
Ivar myIvar = ivarList[i];
const char *ivarName = ivar_getName(myIvar);
NSLog(@"ivar(%d) : %@", i, [NSString stringWithUTF8String:ivarName]);
}
free(ivarList);
objc_property_t *propertyList = class_copyPropertyList([UITextField class], &count);
for (unsigned int i = 0; i < count; i++) {
const char *propertyName = property_getName(propertyList[i]);
NSLog(@"propertyName(%d) : %@", i, [NSString stringWithUTF8String:propertyName]);
}
free(propertyList);
}
// 通过修改 UITextfield 的私有属性更改占位颜色和字体
- (void)createLoginTextField {
UITextField *loginTextField = [[UITextField alloc] init];
loginTextField.frame = CGRectMake(15,(self.view.bounds.size.height-52-50)/2, self.view.bounds.size.width-60-18,52);
loginTextField.delegate = self;
loginTextField.font = [UIFont systemFontOfSize:14];
loginTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
loginTextField.textColor = [UIColor blackColor];
loginTextField.placeholder = @"用户名/邮箱";
[loginTextField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
[loginTextField setValue:[UIColor lightGrayColor]forKeyPath:@"_placeholderLabel.textColor"];
[self.view addSubview:loginTextField];
}
补充,使用runtime在iOS13系统下会报错如下图
解决方法:
//获取一个实例变量信息
Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
//获取成员变量的值
UILabel *placeLabel = object_getIvar(textField, ivar);
placeLabel.textColor = [UIColor systemPinkColor];
placeLabel.font = [UIFont systemFontOfSize:18];
最后一方法转自于此篇华文:https://www.jianshu.com/p/aeecc4b4621c