IOS_改变UITextField placeHolder颜色、字体

IOS_改变UITextField placeHolder颜色、字体

我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围,甚至修改placeHolder颜色,字体。

– textRectForBounds:      //重写来重置文字区域

– drawTextInRect:         //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

– placeholderRectForBounds:  //重写来重置占位符区域

– drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了

– borderRectForBounds:  //重写来重置边缘区域

– editingRectForBounds:  //重写来重置编辑区域

– clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真

– leftViewRectForBounds:

– rightViewRectForBounds:

通过– drawPlaceholderInRect:方法可改变placeHolder颜色、字体,请看代码:

首先定义一个类CustomTextField让它继承UITextField实现以下方法即可:

//控制清除按钮的位置

-(CGRect)clearButtonRectForBounds:(CGRect)bounds

{

return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);

}

//控制placeHolder的位置,左右缩20

-(CGRect)placeholderRectForBounds:(CGRect)bounds

{

//return CGRectInset(bounds, 20, 0);

CGRect inset = CGRectMake(bounds.origin.x+100, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些

return inset;

}

//控制显示文本的位置

-(CGRect)textRectForBounds:(CGRect)bounds

{

//return CGRectInset(bounds, 50, 0);

CGRect inset = CGRectMake(bounds.origin.x+190, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些

return inset;

}

//控制编辑文本的位置

-(CGRect)editingRectForBounds:(CGRect)bounds

{

//return CGRectInset( bounds, 10 , 0 );

CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width -10, bounds.size.height);

return inset;

}

//控制左视图位置

- (CGRect)leftViewRectForBounds:(CGRect)bounds

{

CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width-250, bounds.size.height);

return inset;

//return CGRectInset(bounds,50,0);

}

//控制placeHolder的颜色、字体

- (void)drawPlaceholderInRect:(CGRect)rect

{

//CGContextRef context = UIGraphicsGetCurrentContext();

//CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);

[[UIColororangeColor] setFill];

[[selfplaceholder] drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];

}

//下面是使用CustomTextField的代码,可放在viewDidLoad等方法中

_textField = [[CustomTextField alloc] initWithFrame:CGRectMake(20, 150, 280, 30)];

_textField.placeholder = @"请输入帐号信息";

_textField.borderStyle = UITextBorderStyleRoundedRect;

_textField.textAlignment = UITextAlignmentLeft;

_textField.delegate = self;

_textField.clearButtonMode = UITextFieldViewModeWhileEditing;

_textField.text = @"aa";

UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-iwant-2.png"]];

_textField.leftView = imgv;

_textField.leftViewMode = UITextFieldViewModeAlways;

[self.view addSubview:_textField];

时间: 2024-08-26 00:20:51

IOS_改变UITextField placeHolder颜色、字体的相关文章

[转]改变UITextField placeHolder颜色、字体

本文转载至 http://m.blog.csdn.net/blog/a394318511/8025170 我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为.这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围,甚至修改placeHolder颜色,字体. – textRectForBounds:     //重写来重置文字区域 – drawTextInRect:        //改变绘文字属性.重写时调用super可以按默

(转) 改变UITextField placeHolder颜色、字体 、输入光标位置等

我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为.这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围,甚至修改placeHolder颜色,字体. – textRectForBounds:     //重写来重置文字区域 – drawTextInRect:        //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – placeholderRectFor

改变UITextField placeHolder色彩、字体

改变UITextField placeHolder颜色.字体 我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为.这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围,甚至修改placeHolder颜色,字体. – textRectForBounds:     //重写来重置文字区域 – drawTextInRect:        //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不

修改 UITextfield placeholder 颜色

_password = [[UITextField alloc]init]; _password.font = k18Font; _password.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@" 密码" attributes:@{NSForegroundColorAttributeName:kBlackColor}]; ***************** 而且 UITextField 比 UIL

CSS 改变input placeholder 颜色

input:-ms-input-placeholder { color: #a9a9a9; }/* Internet Explorer 10+ */ input::-webkit-input-placeholder { color: #a9a9a9; }/* WebKit browsers */ input::-moz-placeholder { color: #a9a9a9; }/* Mozilla Firefox 4 to 18 */ input:-moz-placeholder { col

UITextField自定义placeHolder颜色并保持placeHolder居中

思路: 1.自定义UITextField的子类 2.重写drawPlaceholderInRect方法改变placeHolder颜色 3.重写placeholderRectForBounds方法保持placeHolder文字居中 代码如下: @interface CustomPlaceHolderTextField : UITextField @end @implementation CustomPlaceHolderTextField - (void)drawPlaceholderInRect

【代码笔记】UITextField设置placeholder颜色

一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITextFieldDelegate> @end RootViewController.m #import "RootViewController.h" @interface RootViewController () @end @i

【JavaScript】轻易改变的背景和字体颜色页面

JavaScript,点击button改变页面背景和字体颜色,网页有N颜色的变化button.点击不同button,网页字体和背景将被改变为不同的颜色. 非常easy的JavaScript小程序. 一.基本目标 一打开网页首先提示问候信息"你好" 网页中有N个改变颜色的button,当中返回是返回网页的默认颜色,背景是白的,字体是黑的 点击不同的button.网页的字体与背景就会改变成不同的颜色. 本来想做出彩虹起色的,但原理全然一样就不多写button了. 二.基本思想 关键是对bo

IOS 开发更改UITextField的Placeholder颜色

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];    UIColor *color = [UIColor redColor]; textField.backgroundColor = [UIColor yellowColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initW