UILabel詳細應用
一.設置佔位文字的顏色
方法一:利用富文本
/* 手機號輸入框 /
@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
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:
文/YotrolZ(簡書作者)
原文鏈接:http://www.jianshu.com/p/49b4eb97f41e
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。