
珍惜時(shí)間
UITextField是iOS開發(fā)中經(jīng)常使用到的控件,它有一個(gè)placeholder屬性,也就是占位文字。默認(rèn)占位文字顏色是70% gray,但有時(shí)我們可能需要修改其占位文字的顏色,下文中將為大家介紹三中修改方法,并就動(dòng)態(tài)改變顏色做相關(guān)說(shuō)明(關(guān)于動(dòng)態(tài)改變:當(dāng)UITextField為第一響應(yīng)者時(shí)為一種顏色,取消第一響應(yīng)者時(shí)為另一種顏色)。
方法1
- 設(shè)置
attributedPlaceholder屬性 - 說(shuō)明:此種方式對(duì)于無(wú)需動(dòng)態(tài)改變
placeholder顏色較為方便。
- 代碼1(單色)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 創(chuàng)建屬性字典
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:17]; // 設(shè)置font
attrs[NSForegroundColorAttributeName] = [UIColor greenColor]; // 設(shè)置顏色
NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"夏蟲不可以語(yǔ)冰" attributes:attrs]; // 初始化富文本占位字符串
self.textField.attributedPlaceholder = attStr;
-
效果1
單色placeholder 代碼2(復(fù)色)
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"夏蟲不可以語(yǔ)冰"];
[attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:15.0]} range:NSMakeRange(0, 2)];
[attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor],
NSFontAttributeName : [UIFont systemFontOfSize:17.0]} range:NSMakeRange(2, 3)];
[attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor],
NSFontAttributeName : [UIFont systemFontOfSize:15.0]} range:NSMakeRange(5, 2)];
self.textField.attributedPlaceholder = attStr;
-
效果
復(fù)色placeholder
方法2
- 自定義UITextField,重寫
- (void)drawPlaceholderInRect:(CGRect)rect; - 說(shuō)明:此種方式只能設(shè)置一次狀態(tài),不能動(dòng)態(tài)的改變
placeholder的顏色,但可以設(shè)置placeholder所在位置。
- 代碼
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeholder drawInRect:CGRectMake(0, 2, rect.size.width, 25) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName : [UIColor blueColor],
}];
}
-
效果
placeholder
方法3
- 自定義UITextField,利用runTime找出UITextFiled內(nèi)部隱藏的成員變量和屬性,利用KVC進(jìn)行修改。
- 說(shuō)明:此種方式對(duì)于動(dòng)態(tài)改變
placeholder顏色較為方便。
- 拓展代碼(利用runTime找出成員變量和屬性,程序中無(wú)需使用,只是幫助我們看清UITextField內(nèi)部結(jié)構(gòu),知道其中的相關(guān)成員變量和屬性,然后賦值即可)。
#import "SJTextField.h"
#import <objc/runtime.h>
@implementation SJTextField
// 初始化調(diào)用一次 用于查看UITextField中的成員屬性和變量
+ (void)initialize
{
[self getIvars];
// [self getProperties];
}
// 獲取所有屬性
+ (void)getProperties
{
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([UITextField class], &count);
for (int i = 0; i<count; i++) {
// 取出屬性
objc_property_t property = properties[i];
// 打印屬性名字
NSLog(@"%s<---->%s", property_getName(property), property_getAttributes(property));
}
free(properties);
}
// 獲取所有成員變量
+ (void)getIvars
{
unsigned int count = 0;
// 拷貝出所有的成員變量列表
Ivar *ivars = class_copyIvarList([UITextField class], &count);
for (int i = 0; i<count; i++) {
// 取出成員變量
// Ivar ivar = *(ivars + i);
Ivar ivar = ivars[i];
// 打印成員變量名字
NSLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
}
// 釋放
free(ivars);
}
@end
-
拓展點(diǎn) 可以查到有兩個(gè)關(guān)于
placeholder的屬性和變量,分別是_placeholderLabel.textColor和_placeholderLabel,故下面就是用來(lái)設(shè)置動(dòng)態(tài)改變placeholder顏色的代碼。 - 代碼(在自定義UITextField中)
#import "SJTextField.h"
#import <objc/runtime.h>
@implementation SJTextField
static NSString * const SJPlacerholderColorKeyPath = @"_placeholderLabel.textColor";
- (void)awakeFromNib
{
// 設(shè)置placeholder開始顏色(方式一)
// UILabel *placeholderLabel = [self valueForKeyPath:@"_placeholderLabel"];
// placeholderLabel.textColor = [UIColor redColor];
// 設(shè)置placeholder開始顏色(方式二)
[self setValue:[UIColor greenColor] forKeyPath:SJPlacerholderColorKeyPath];
// 不成為第一響應(yīng)者
[self resignFirstResponder];
}
/**
* 當(dāng)前文本框聚焦時(shí)就會(huì)調(diào)用
*/
- (BOOL)becomeFirstResponder
{
// 修改占位文字顏色
[self setValue:[UIColor redColor] forKeyPath:SJPlacerholderColorKeyPath];
return [super becomeFirstResponder];
}
/**
* 當(dāng)前文本框失去焦點(diǎn)時(shí)就會(huì)調(diào)用
*/
- (BOOL)resignFirstResponder
{
// 修改占位文字顏色
[self setValue:[UIColor greenColor] forKeyPath:SJPlacerholderColorKeyPath];
return [super resignFirstResponder];
}
@end
- 效果

未進(jìn)入編輯狀態(tài)

進(jìn)入編輯狀態(tài)


