http://www.jianshu.com/p/e5c6c9b455dd
第一种方法
[_tfdUserName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
第二种方法全改
#import <UIKit/UIKit.h>
@interface UITextField (Placeholder)
@property UIColor *placeholderColor;
@end
#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);
}
- (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
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有