您的位置:首页 > 移动开发 > IOS开发

iOS设置TextField的placeholder的颜色,位置,字体,光标颜色

2017-02-15 17:33 645 查看


前言

由于项目需要修改TextField的占位符的颜色,位置等,总结下如何设置UITextField的占位符的一些属性。


一.设置placeholder的颜色字体

1.iOS6.0之后苹果提供了attributedPlaceholder属性可以设置
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。
// 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文件
#import <UIKit/UIKit.h>
@interface ZYTextField : UITextField
@end

.m文件
#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.纯代码创建UITextField
UITextField *textField = [[ZYTextField alloc]initWithFrame:CGRectMake(0, 300, 300, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];

b.使用xib或者storyboard创建

修改UITextField的类属性



使用xib或者storyboard创建

.png

3.实际运行效果图



运行效果.png
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: