一.設置占位文字的顏色
方法一:利用富文本
/** 手機號輸入框 */ @property (weak, nonatomic) IBOutlet UITextField *phoneTextField; - (void)viewDidLoad { [super viewDidLoad]; // 創建一個富文本對象 NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; // 設置富文本對象的顏色 attributes[NSForegroundColorAttributeName] = [UIColor whiteColor]; // 設置UITextField的占位文字 self.phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手機號" attributes:attributes]; }
方法二:利用Runtime
獲取私有的屬性名稱,利用KVC
設置屬性
// 設置占位文字的顏色為紅色(注意下面的'self'代表你要修改占位文字的UITextField控件)
[self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
- 注意:
_placeholderLabel.textColor
是不可亂寫的哦,我們是怎么獲取到這個屬性的呢?請看下文:// 只調用一次(自定義UITextField) + (void)initialize { [self getIvars]; } // 獲取私有變量名稱 + (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar)); } }
查看打印,找出可能的屬性名稱,試試便知;
- 完整代碼:自定義的
UITextField
,獲取到焦點(編輯狀態)的時候是白色,失去焦點(非編輯狀態)的時候是灰色:#import "YCTextField.h" #import <objc/runtime.h> #define YCplaceholderTextColor @"_placeholderLabel.textColor" @implementation YCTextField + (void)initialize { [self getIvars]; } // 獲取私有變量名稱 + (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar)); } } - (void)awakeFromNib { // 設置光標的顏色 self.tintColor = self.textColor; } // 獲取到焦點 - (BOOL)becomeFirstResponder { // 利用運行時獲取key,設置占位文字的顏色 [self setValue:self.textColor forKeyPath:YCplaceholderTextColor]; return [super becomeFirstResponder]; } // 失去焦點 - (BOOL)resignFirstResponder { // 利用運行時獲取key,設置占位文字的顏色 [self setValue:[UIColor grayColor] forKeyPath:YCplaceholderTextColor]; return [super resignFirstResponder]; } @end
方法三.將占位文字
畫
上去(重寫- (void)drawPlaceholderInRect:(CGRect)rect;
)- (void)drawPlaceholderInRect:(CGRect)rect { [[UIColor orangeColor] set]; [self.placeholder drawInRect:rect withFont:[UIFont systemFontOfSize:20]]; }
二.設置光標顏色
// 設置光標的顏色 self.tintColor = [UIColor redColor];
三.設置占位文字的偏移
- 重寫
-(CGRect)placeholderRectForBounds:(CGRect)bounds;
方法 -
可以用來設置光標與占位的間距
//控制placeHolder的位置,左右縮20 -(CGRect)placeholderRectForBounds:(CGRect)bounds { //return CGRectInset(bounds, 20, 0); CGRect inset = CGRectMake(bounds.origin.x+50, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些 return inset; }
擴充:系統還提供了很多類似的方法 – textRectForBounds: //重寫來重置文字區域 – drawTextInRect: //改變繪文字屬性.重寫時調用super可以按默認圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了. – placeholderRectForBounds: //重寫來重置占位符區域 – drawPlaceholderInRect: //重寫改變繪制占位符屬性.重寫時調用super可以按默認圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了 – borderRectForBounds: //重寫來重置邊緣區域 – editingRectForBounds: //重寫來重置編輯區域 – clearButtonRectForBounds: //重寫來重置clearButton位置,改變size可能導致button的圖片失真 – leftViewRectForBounds: – rightViewRectForBounds:
原文鏈接:https://blog.csdn.net/u012907783/article/details/53157685
- 重寫
- 完整代碼:自定義的