iOS設(shè)置TextField的placeholder的顏色,位置,字體,光標(biāo)顏色

前言

由于項(xiàng)目需要修改TextField的占位符的顏色,位置等,總結(jié)下如何設(shè)置UITextField的占位符的一些屬性。

一.設(shè)置placeholder的顏色字體

1.iOS6.0之后蘋果提供了attributedPlaceholder屬性可以設(shè)置

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
NSString *holderText = @"這個(gè)是placeholder";
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
[placeholder addAttribute:NSForegroundColorAttributeName
                        value:[UIColor greenColor]
                        range:NSMakeRange(0, holderText.length)];
[placeholder addAttribute:NSFontAttributeName
                        value:[UIFont boldSystemFontOfSize:15]
                        range:NSMakeRange(0, holderText.length)];
textField.attributedPlaceholder = placeholder;
[self.view addSubview:textField];

2.通過KVC訪問內(nèi)部變量直接設(shè)置

textField.placeholder = @"手機(jī)號(hào)碼"; 
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

二.設(shè)置placeholder的文本的位置

蘋果給我們提供了以下方法可以自定義一個(gè)TextField,我們可以重寫這些方法定制自己的UITextField。

// drawing and positioning overrides
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;

1.下面是自定義的一個(gè)UITextField類,根據(jù)自己的需求進(jìn)行定制
.h文件

#import <UIKit/UIKit.h>
@interface ZYTextField : UITextField
@end

.m文件

#define Default_FontColor ZYRGBColor(77, 150, 132)
#import "ZYTextField.h"
@implementation ZYTextField
//通過代碼創(chuàng)建
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}
//通過xib創(chuàng)建
-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setUpUI];
}

- (void)setUpUI
{
//    設(shè)置border
//    self.layer.masksToBounds = YES;
//    self.layer.cornerRadius = 22;
//    self.backgroundColor = Default_FontColor;
//    self.layer.borderColor = [UIColor blackColor].CGColor;
//    self.layer.borderWidth = 1;
    //字體大小
    self.font = [UIFont systemFontOfSize:15];
    //字體顏色
    self.textColor = Default_FontColor;
    //光標(biāo)顏色
    self.tintColor= self.textColor;
    //占位符的顏色和大小
    [self setValue:ZYRGBColor(167, 167, 167) forKeyPath:@"_placeholderLabel.textColor"];
    [self setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    // 不成為第一響應(yīng)者
    [self resignFirstResponder];
}
/**
 * 當(dāng)前文本框聚焦時(shí)就會(huì)調(diào)用
 */
- (BOOL)becomeFirstResponder
{
    // 修改占位文字顏色
    [self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];
    return [super becomeFirstResponder];
}

/**
 * 當(dāng)前文本框失去焦點(diǎn)時(shí)就會(huì)調(diào)用
 */
- (BOOL)resignFirstResponder
{
    // 修改占位文字顏色
    [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    return [super resignFirstResponder];
}
//控制placeHolder的位置
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
    return inset;
}

//控制顯示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x+15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
    return inset;
}

//控制編輯文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake(bounds.origin.x +15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
    return inset;
}

2.使用方法
a.純代碼創(chuàng)建UITextField

UITextField *textField = [[ZYTextField alloc]initWithFrame:CGRectMake(0, 300, 300, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];

b.使用xib或者storyboard創(chuàng)建
修改UITextField的類屬性


使用xib或者storyboard創(chuàng)建 .png

3.實(shí)際運(yùn)行效果圖

運(yùn)行效果.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容