我们有时需要定制化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];

(转) 改变UITextField placeHolder颜色、字体 、输入光标位置等的更多相关文章

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

    本文转载至 http://m.blog.csdn.net/blog/a394318511/8025170 我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段 ...

  2. IOS_改变UITextField placeHolder颜色、字体

    http://blog.sina.com.cn/s/blog_671d2e4f0101d90v.html

  3. 改变UITextField placeHolder色彩、字体

    改变UITextField placeHolder颜色.字体 我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为.这些方法都会返回一个CGRect ...

  4. Demo 示例控制输入光标位置

    <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <meta name="aut ...

  5. chrome、firefox、IE中input输入光标位置错位解决方案

    以前在项目里碰到过一个问题 input输入框用一个背景图模拟,设置height和line-height一样的高度,使里面的输入文字能够居中, 在FF下出现的情况是:点击input时,输入光标其实上跟i ...

  6. 改变UITextField placeHolder 字体 颜色

    [_textSearchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; ...

  7. Swift 修改UITextField.Placeholder颜色

    StoreNameEditTextField.attributedPlaceholder = NSAttributedString(string:"点此处输入门店名称",attri ...

  8. iOS 更改uitextfield placeholder颜色

    [passwordField setValue:TPColor forKeyPath:@"_placeholderLabel.textColor"];

  9. 在屏幕中间显示,按ESC键后改变字符的颜色

    程序功能:编写程序在屏幕中间显示“a”~“z”,并可以让人看清,这个任务比较好实现. (1)在b800:[ 160*12+40*2]处存入a的ASCII码.(2)在循环中使用一个10000000000 ...

随机推荐

  1. linux系统任务调度命令crontab

    循环重复的执行计划任务.有计划性的执行任务,像这种任务,在linux系统中就有cron命令来完成. linux系统下的任务调度分为两类:系统任务调度和用户任务调度. /etc/crontab文件就是系 ...

  2. 转:在linux中vi 文件里行尾奇怪的^M及解决

    Linux编辑器vim中删除行尾的^M 有时候,在 Linux 中使用打开曾在 Windows 中编辑过的文件时,会在行尾看到 ^M 字符.看起来总是感觉很别扭. 删除方法如下: 在 Vim 的命令模 ...

  3. 代码中看见一共8个变量参数{SEO,0,0,0,0,0,0,0} 解读!{Top,0,0,0,0,0,0,Top}{Nav,0,0,0,0,0,0,Nav}

    代码中看见{SEO,0,0,0,0,0,0,0}{Top,0,0,0,0,0,0,Top}{Nav,0,0,0,0,0,0,Nav}解读! 举个例子: {GetNew,977,0,23,500,0,0 ...

  4. js实现文本框验证和实现小数的加减乘除

    <script type="text/javascript"> //加法 var m=accAdd(1.22,1.22); //减法 var m1=accSub(1.2 ...

  5. Java 语言中一个字符占几个字节?

    Java中理论说是一个字符(汉字 字母)占用两个字节. 但是在UTF-8的时候 new String("字").getBytes().length 返回的是3 表示3个字节 作者: ...

  6. MySQL性能优化奇技淫巧

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.   2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使 ...

  7. python GIL锁问题

    一.GIL是什么 官方解释: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple na ...

  8. jquery分页组件(每页显示多少条)

    /** * 功能说明:jPager 分页插件 * 参数说明:pages:[] 分页的控件个数 @id:显示分页的div ID,@showSelectPage: 是否显示当前分页的条目过滤下拉框 * @ ...

  9. Android(java)学习笔记151:Android中操作JSON数据(Json和Jsonarray)

    1.Json 和 Xml       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...

  10. couldn't be opened because you don't have permission to view it” 解决方法

    I use Xcode6 GM. I encountered the same problem. What I did was to go to Build Options. Then I chang ...