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"];
苹果给咱们提供了如下方法能够自定义一个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
3.实际运行效果图it
做者:Z了个Y连接:http://www.jianshu.com/p/db8773b13388來源:简书著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。