1、通過屬性
a、 ??//文字屬性(一般)
? NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
? attrs[NSForegroundColorAttributeName] = [UIColor blueColor];
NSAttributedString *placeholderStr = [[NSAttributedString alloc] initWithString:@"手機號" attributes:attrs];
? ?self.phoneTextField.attributedPlaceholder = placeholderStr;
?b、稍微高級一點
? ??NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"手機號"];
? ? [placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName : [UIFont systemFontOfSize:20]
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } range:NSMakeRange(0, 1)];
? ? [placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} range:NSMakeRange(1, 1)];
? ? [placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:NSMakeRange(2, 1)];
? ? self.phoneTextField.attributedPlaceholder = placeholder;
?
二、通過重寫UITextField的方法
繼承UITextField的類,
- (void)drawPlaceholderInRect:(CGRect)rect
{
? ? [self.placeholder drawInRect:CGRectMake(10, 10, 10, 1) withAttributes:@{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSForegroundColorAttributeName :[UIColor blueColor],
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName :[UIFont systemFontOfSize:10]
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];
}
使用的時候,如果是xib創建的textField,就吧xib中的textField的類名改成這個自定義的,如果是代碼創建,就用這個自定義的textField去創建。
三、或者在UITextField中放個label,輸入時隱藏,也可以達到效果
四、通過Runtime更改
?運行時(Runtime):
?* 蘋果官方一套C語言庫
?* 能做很多底層操作(比如訪問隱藏的一些成員變量\成員方法....)
?
#import "LHBTestTextField.h"
//要用運行時,必須導入該庫
#import <objc/runtime.h>
static NSString * const LHBPlacerholderColorKeyPath = @"_placeholderLabel.textColor";
@implementation LHBTestTextField
//讓代碼創建的TextField的也可以用
- (instancetype)initWithFrame:(CGRect)frame
{
? ? if (self = [super initWithFrame:frame]) {
?? ? ? ?
? ? }
? ? return self;
}
?- (void)awakeFromNib
{
//? ? 可以改
//? ? UILabel *plcaeholderLabel = [self valueForKeyPath:@"_placeholderLabel"];
//? ? plcaeholderLabel.textColor = [UIColor blueColor];
? ?
//? ? 或者一句代碼
//? ? [self setValue:[UIColor yellowColor] forKeyPath:@"_placeholderLabel.textColor"];
?? ?
//? ? 設置光標顏色
//? ? self.tintColor = [UIColor orangeColor];
//? ? self.tintColor = self.textColor;//和文字顏色一致
? ??? ??
? ? //在RiderGirl中的應用
? ? [self resignFirstResponder];
? ? self.tintColor = [UIColor yellowColor];
}
#pragma mark - 當輸入框聚焦時,調用,更改顏色。重寫第一響應方法
- (BOOL)becomeFirstResponder
{
? ? //成為第一響應,就修改placeholder的文字顏色
? ? [self setValue:self.textColor forKeyPath:LHBPlacerholderColorKeyPath];
? ? return [super becomeFirstResponder];
}
?
#pragma mark - 當焦點離開輸入框時,調用,更改顏色。重寫失去焦點方法
- (BOOL)resignFirstResponder
{
? ? [self setValue:[UIColor grayColor] forKeyPath:LHBPlacerholderColorKeyPath];
? ? return [super resignFirstResponder];
}
+ (void)initialize
{
//? ? [self getIvars];
}
?
+ (void)getProperties
{
? ? unsigned int count = 0;
? ? //相當于拷貝出來,要手動管理內存
? ? objc_property_t *propreties = class_copyPropertyList([UITextField class], &count);
? ? for (NSInteger i=0; i<count; i++) {
? ? ? ? //取出屬性
? ? ? ? objc_property_t property = propreties[i];
? ? ? ? //打印屬性名字
? ? ? ? NSLog(@"%s --- %s",property_getName(property),property_getAttributes(property));
? ? }
? ? //釋放內存
? ? free(propreties);
?
}
?
+ (void)getIvars
{
? ? unsigned int count = 0;
? ? //相當于拷貝所有成員變量,要手動管理內存
? ? Ivar *ivars = class_copyIvarList([UITextField class], &count);
? ? for (NSInteger i=0; i<count; i++) {
? ? ? ? //取出成員變量
//? ? ? ? Ivar ivar = *(ivars + i);
? ? ? ? Ivar ivar = ivars[i];//指向的是數組首元素時,可以當數組來用
? ? ? ?
? ? ? ? //打印一下看看UITextField里面隱藏的成員變量的名字
? ? ? ? NSLog(@"%s --- %s",ivar_getName(ivar),ivar_getTypeEncoding(ivar));
? ? }
? ? //釋放內存
? ? free(ivars);
? ? ??//會打印出一堆成員變量的名字,拿到這個成員變量的名字,可以通過kvc來更改內部屬性,現在可以拿到_placeholderLabel
}
?
#pragma mark - 該方法只能實現高亮時更改placeholder的顏色。焦點離開時暫時改不了
//- (void)setHighlighted:(BOOL)highlighted
//{
//? ? [self setValue:self.textColor forKeyPath:LHBPlacerholderColorKeyPath];
//}
?
?
#pragma mark - 寫框架的話,可以在外部加一屬性,重寫set方法,方便外部更改
?- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
? ??_placeholderColor = placeholderColor;
? ? [self setValue:placeholderColor forKeyPath:LHBPlacerholderColorKeyPath];
}