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

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

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

前言

最近學(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的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機制,找到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重寫setget方法。

總結(jié)

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

相關(guān)文章

最新評論

海口市| 东丽区| 望江县| 左云县| 遵义县| 遂川县| 商城县| 宁德市| 永定县| 兴和县| 威宁| 沁阳市| 屏山县| 晴隆县| 保德县| 广德县| 大渡口区| 伽师县| 江山市| 嘉兴市| 江口县| 开封县| 丰台区| 龙井市| 牡丹江市| 慈溪市| 旬阳县| 水城县| 梁山县| 股票| 灌阳县| 锡林郭勒盟| 沙雅县| 卢龙县| 云阳县| 滦平县| 黄大仙区| 宁波市| 洪江市| 孝昌县| 克拉玛依市|