iOS設置TextField的placeholder的顏色,位置,字體,光標顏色

轉載自:Z了個Y   簡書

 

一.設置placeholder的顏色字體

1.iOS6.0以後蘋果提供了attributedPlaceholder屬性能夠設置ide

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 200, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
NSString *holderText = @"這個是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訪問內部變量直接設置字體

textField.placeholder = @"手機號碼"; 
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

二.設置placeholder的文本的位置

蘋果給咱們提供瞭如下方法能夠自定義一個TextField,咱們能夠重寫這些方法定製本身的UITextField。3d

// 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.下面是自定義的一個UITextField類,根據本身的需求進行定製
.h文件code

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

.m文件blog

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

- (void)setUpUI
{
//    設置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;
    //光標顏色
    self.tintColor= self.textColor;
    //佔位符的顏色和大小
    [self setValue:ZYRGBColor(167, 167, 167) forKeyPath:@"_placeholderLabel.textColor"];
    [self setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    // 不成爲第一響應者
    [self resignFirstResponder];
}
/**
 * 當前文本框聚焦時就會調用
 */
- (BOOL)becomeFirstResponder
{
    // 修改佔位文字顏色
    [self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];
    return [super becomeFirstResponder];
}

/**
 * 當前文本框失去焦點時就會調用
 */
- (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.純代碼建立UITextFieldip

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

b.使用xib或者storyboard建立
修改UITextField的類屬性get


使用xib或者storyboard建立
.png


3.實際運行效果圖it

做者:Z了個Y連接:http://www.jianshu.com/p/db8773b13388來源:簡書著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
相關文章
相關標籤/搜索