在我们的开发中,有时候会遇到背景深色的界面上需要添加TextField,TextField默认的占位颜色也是深灰色,比较难看清,这时候就需要修改展位文字的颜色,可是系统没有提供相应的方法,那么就需要我们自己来自定义了,废话不多说,下面上代码:
// .h文件
#import <UIKit/UIKit.h>
@interface UITextField (Placeholder)
@property UIColor *placeholderColor;
@end
// .m文件
#import "UITextField+Placeholder.h"
#import <objc/message.h>
NSString * const placeholderColorName = @"placeholderColor";
@implementation UITextField (Placeholder)
+ (void)load
{
// 获取setPlaceholder
Method setPlaceholder = class_getInstanceMethod(self, @selector(setPlaceholder:));
// 获取bs_setPlaceholder
Method bs_setPlaceholder = class_getInstanceMethod(self, @selector(bs_setPlaceholder:));
// 交换方法
method_exchangeImplementations(setPlaceholder, bs_setPlaceholder);
}
// OC最喜欢懒加载,用的的时候才会去加载
// 需要给系统UITextField添加属性,只能使用runtime
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
// 设置关联
objc_setAssociatedObject(self,(__bridge const void *)(placeholderColorName), placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// 设置占位文字颜色
UILabel *placeholderLabel = [self valueForKeyPath:@"placeholderLabel"];
placeholderLabel.textColor = placeholderColor;
}
- (UIColor *)placeholderColor
{
// 返回关联
return objc_getAssociatedObject(self, (__bridge const void *)(placeholderColorName));
}
// 设置占位文字,并且设置占位文字颜色
- (void)bs_setPlaceholder:(NSString *)placeholder
{
// 1.设置占位文字
[self bs_setPlaceholder:placeholder];
// 2.设置占位文字颜色
self.placeholderColor = self.placeholderColor;
}
@end
网友评论
何必这么麻烦?