iOS中修改UITextField占位符字體顏色的方法總結(jié)
前言
最近學(xué)了UITextField控件, 感覺在里面設(shè)置占位符非常好, 給用戶提示信息, 于是就在想占位符的字體和顏色能不能改變呢?下面是小編的一些簡單的實現(xiàn),有需要的朋友們可以參考。
修改UITextField的占位符文字顏色主要有三個方法:
1、使用attributedPlaceholder屬性
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil
2、重寫drawPlaceholderInRect方法
- (void)drawPlaceholderInRect:(CGRect)rect;
3、修改UITextField內(nèi)部placeholderLaber的顏色
[textField setValue:[UIColor grayColor] forKeyPath@"placeholderLaber.textColor"];
以下是詳細(xì)的實現(xiàn)過程
給定場景,如在注冊登錄中,要修改手機號和密碼TextField的placeholder的文字顏色。
效果對比

使用前

使用后
使用attributedPlaceholder
自定義GYLLoginRegisterTextField類,繼承自UITextField;實現(xiàn)awakeFromNib()方法,如果使用storyboard,那么修改對應(yīng)的UITextField的CustomClass為GYLLoginRegisterTextField即可
具體代碼如下:
#import "GYLLoginRegisterTextField.h"
@implementation GYLLoginRegisterTextField
- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //設(shè)置光標(biāo)顏色
//修改占位符文字顏色
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attrs];
}
@end
重寫drawPlaceholderInRect方法
與方法一同樣,自定義GYLLoginRegisterTextField,繼承自UITextField,重寫drawPlaceholderInRect方法,后續(xù)相同
代碼如下:
#import "GYLLoginRegisterTextField.h"
@implementation GYLLoginRegisterTextField
- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //設(shè)置光標(biāo)顏色
}
- (void)drawPlaceholderInRect:(CGRect)rect
{
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
attrs[NSFontAttributeName] = self.font;
//畫出占位符
CGRect placeholderRect;
placeholderRect.size.width = rect.size.width;
placeholderRect.size.height = rect.size.height;
placeholderRect.origin.x = 0;
placeholderRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5;
[self.placeholder drawInRect:placeholderRect withAttributes:attrs];
//或者
/*
CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5);
[self.placeholder drawAtPoint:placeholderPoint withAttributes:attrs];
*/
}
@end
修改UITextField內(nèi)部placeholderLaber的顏色
使用KVC機制,找到UITextField內(nèi)部的修改站位文字顏色的屬性:placeholderLaber.textColor
代碼如下:
#import "GYLLoginRegisterTextField.h"
@implementation GYLLoginRegisterTextField
- (void)awakeFromNib
{
self.tintColor = [UIColor whiteColor]; //設(shè)置光標(biāo)顏色
//修改占位符文字顏色
[self setValue:[UIColor grayColor] forKeyPath@"placeholderLaber.textColor"];
}
@end
第三種方法比較簡單,建議可以將此封裝:擴展UITextField,新建category,添加placeholderColor屬性,使用KVC重寫set和get方法。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望能對大家開發(fā)iOS有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
iOS實現(xiàn)支持小數(shù)的星星評分組件實例代碼
程序中需要打分的功能,在網(wǎng)上找了幾個,都不是很滿意。所以自己動手實現(xiàn)了一個,下面這篇文章主要給大家介紹了關(guān)于利用iOS實現(xiàn)支持小數(shù)的星星評分組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
iOS App開發(fā)中使用設(shè)計模式中的單例模式的實例解析
單例模式是最簡單和基本的一種設(shè)計模式,下面我們就簡單解讀一下iOS中單例設(shè)計模式的用法,示例代碼還是為傳統(tǒng)的Objective-C,主要為了體現(xiàn)單例模式的思想,需要的朋友可以參考下2016-05-05
iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例
這篇文章主要介紹了iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例,CGContextRef同時可以進(jìn)行圖形顏色的填充以及文字的書寫,需要的朋友可以參考下2016-05-05
Objective-C基礎(chǔ) 自定義對象歸檔詳解及簡單實例
這篇文章主要介紹了Objective-C基礎(chǔ) 自定義對象歸檔詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS實現(xiàn)UITableView左滑刪除復(fù)制即用功能
這篇文章主要介紹了iOS實現(xiàn)UITableView左滑刪除復(fù)制即用功能,在項目開發(fā)中經(jīng)常會用到這樣的需求,下面小編把實現(xiàn)代碼分享給大家,需要的朋友可以參考下2017-09-09

