最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS中修改UITextField占位符字體顏色的方法總結(jié)

 更新時(shí)間:2016年09月23日 11:32:26   作者:頑童大了已沒那么笨  
這篇文章給大家分享了iOS中修改UITextField占位符字體顏色的三個(gè)方法,分別是使用attributedPlaceholder屬性、重寫drawPlaceholderInRect方法和修改UITextField內(nèi)部placeholderLaber的顏色,下面我們一起來看看詳細(xì)的方法介紹。

前言

最近學(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)過程

給定場(chǎng)景,如在注冊(cè)登錄中,要修改手機(jī)號(hào)和密碼TextField的placeholder的文字顏色。

效果對(duì)比


使用前


使用后

使用attributedPlaceholder

自定義GYLLoginRegisterTextField類,繼承自UITextField;實(shí)現(xiàn)awakeFromNib()方法,如果使用storyboard,那么修改對(duì)應(yīng)的UITextField的CustomClassGYLLoginRegisterTextField即可

具體代碼如下:

#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重寫setget方法。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望能對(duì)大家開發(fā)iOS有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論

南通市| 久治县| 威海市| 岳西县| 长春市| 习水县| 岳阳县| 韩城市| 蓝山县| 平安县| 徐汇区| 上思县| 邻水| 赤城县| 资中县| 林周县| 新绛县| 九龙城区| 枝江市| 江阴市| 勃利县| 延庆县| 平乐县| 丰城市| 逊克县| 竹北市| 南投县| 永春县| 元谋县| 沂源县| 双城市| 云安县| 塔城市| 衡东县| 聂荣县| 台安县| 夹江县| 牟定县| 连山| 昌平区| 牡丹江市|