我们有时需要定制化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. 094 Binary Tree Inorder Traversal 中序遍历二叉树

    给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...

  2. [已读]JavaScript DOM编程艺术

    看到过很多人将它作为推荐入门书籍,当时我刚看完ppk和javascript精粹,于是看到这本就觉得很一般了.怎么说,它适合基础.

  3. MySQL简单的确定瓶颈

    如果接到报警可能需要ssh看看瓶颈是什么,怎么下手 确定os层 确定磁盘是否够用的:df –h 再看看系统整体状态: top 哪些进程占用资源比较多,能杀就杀 系统的负载 vmstat看看wa值,r列 ...

  4. linux增加系统监视器的快捷键

    系统命令:gnome-system-monitor 可在终端输入调用 在系统相应的快捷键设置区设置即可

  5. Android线程池(转)

    .前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52415337 使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用, ...

  6. https域名强弱校验的区别

    HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){ public boolean verify(String ...

  7. tomcat 发布本地文件

    应用场景,通过web,jsp访问本地mouse文件夹的静态文件 通过修改tomcat配置文件server.xml <!--在Host标签下加入Context标签,path指的是服务器url请求地 ...

  8. POJ 1655 Balancing Act (树的重心,常规)

    题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...

  9. COGS 2084. Asm.Def的基本算法

    ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “有句美国俗语说,如果走起来像鸭子,叫起来像 ...

  10. hadoop中修改端口号

    1.hdfs-site.xml 这里修改hdfs相关的端口. 1 <property> 2 <name>dfs.namenode.scondary.http-address&l ...