iOS中修改UITextField占位符字體顏色的方法總結(jié)
前言
最近學(xué)了UITextField控件, 感覺在里面設(shè)置占位符非常好, 給用戶提示信息, 于是就在想占位符的字體和顏色能不能改變呢?下面是小編的一些簡(jiǎn)單的實(shí)現(xiàn),有需要的朋友們可以參考。
修改UITextField的占位符文字顏色主要有三個(gè)方法:
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ì)的實(shí)現(xiàn)過(guò)程
給定場(chǎng)景,如在注冊(cè)登錄中,要修改手機(jī)號(hào)和密碼TextField的placeholder的文字顏色。
效果對(duì)比

使用前

使用后
使用attributedPlaceholder
自定義GYLLoginRegisterTextField類,繼承自UITextField;實(shí)現(xiàn)awakeFromNib()方法,如果使用storyboard,那么修改對(duì)應(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機(jī)制,找到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
第三種方法比較簡(jiǎn)單,建議可以將此封裝:擴(kuò)展UITextField,新建category,添加placeholderColor屬性,使用KVC重寫set和get方法。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望能對(duì)大家開發(fā)iOS有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
iOS實(shí)現(xiàn)支持小數(shù)的星星評(píng)分組件實(shí)例代碼
程序中需要打分的功能,在網(wǎng)上找了幾個(gè),都不是很滿意。所以自己動(dòng)手實(shí)現(xiàn)了一個(gè),下面這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)支持小數(shù)的星星評(píng)分組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08
iOS App開發(fā)中使用設(shè)計(jì)模式中的單例模式的實(shí)例解析
單例模式是最簡(jiǎn)單和基本的一種設(shè)計(jì)模式,下面我們就簡(jiǎn)單解讀一下iOS中單例設(shè)計(jì)模式的用法,示例代碼還是為傳統(tǒng)的Objective-C,主要為了體現(xiàn)單例模式的思想,需要的朋友可以參考下2016-05-05
IOS開發(fā)實(shí)現(xiàn)手機(jī)震動(dòng)的提示實(shí)例代碼
這篇文章主要介紹了IOS開發(fā)實(shí)現(xiàn)手機(jī)震動(dòng)的提示實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS 水波紋動(dòng)畫的實(shí)現(xiàn)效果
本篇文章主要介紹了iOS 水波紋的實(shí)現(xiàn)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例
這篇文章主要介紹了iOS App開發(fā)中用CGContextRef繪制基本圖形的基本示例,CGContextRef同時(shí)可以進(jìn)行圖形顏色的填充以及文字的書寫,需要的朋友可以參考下2016-05-05
Objective-C基礎(chǔ) 自定義對(duì)象歸檔詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Objective-C基礎(chǔ) 自定義對(duì)象歸檔詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS實(shí)現(xiàn)UITableView左滑刪除復(fù)制即用功能
這篇文章主要介紹了iOS實(shí)現(xiàn)UITableView左滑刪除復(fù)制即用功能,在項(xiàng)目開發(fā)中經(jīng)常會(huì)用到這樣的需求,下面小編把實(shí)現(xiàn)代碼分享給大家,需要的朋友可以參考下2017-09-09
IOS 避免self循環(huán)引用的方法的實(shí)例詳解
這篇文章主要介紹了IOS 避免self循環(huán)引用的方法的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09

