本文转载至 http://m.blog.csdn.net/blog/a394318511/8025170

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

转自:http://blog.csdn.net/leeyehong_self/article/details/7939147

[转]改变UITextField placeHolder颜色、字体的更多相关文章

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

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

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

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

  3. 改变UITextField placeHolder色彩、字体

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

  4. 改变UITextField placeHolder 字体 颜色

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

  5. Swift 修改UITextField.Placeholder颜色

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

  6. iOS 更改uitextfield placeholder颜色

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

  7. UITextField里面的 placeholder颜色和字体

    //  placeholder 颜色 [field setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor& ...

  8. UITextfield设置Placeholder颜色 控件 内边距、自适应高度

    //创建UITextField对象 UITextField * tf=[[UITextField alloc]init];    //设置Placeholder颜色 [text setAttribut ...

  9. iOS-改变UITextField的Placeholder颜色的三种方式

    转自:http://blog.csdn.net/mazy_ma/article/details/51775670 有时,UITextField自带的Placeholder的颜色太浅或者不满足需求,所以 ...

随机推荐

  1. Linux命令-帮助命令:whatis,apropos

    whatis可以查看命令简化版的帮助内容 whatis ls 查看简化版的ls命令的帮助内容 whatis ifconfig 查看简化版的ifconfig命令的帮助内容 apropos可以查看配置文件 ...

  2. sql 百分比

    select [city], bfb=cast(cast(count(*)*100./(select count(*) from [UserBasicInfo]) as decimal(10,0)) ...

  3. Win10 环境安装tesseract-ocr 4.00并配置环境变量

    Tesseract-OCR的Training简明教程 https://blog.csdn.net/blueheart20/article/details/53207176 一.安装: 选择对应版本,h ...

  4. 数据流图(DFD)画法

    数据流图(DFD)画法要求 一.数据流图(DFD) 1.数据流图的基本符号 数据流图由四种基本符号组成,见图5-4-1所示. 图5-4-1  数据流图的基本符号 例:图5-4-2是一个简单的数据流图, ...

  5. mysql-connector-python 源码安装

    一.下载mysql-connector-python的源码包: 下载页面: https://dev.mysql.com/downloads/connector/python/ 我这下载的是mysql- ...

  6. Memcached真的过时了吗?

    Memcached真的过时了吗? 这两年Redis火得可以,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis真的在 ...

  7. centos系统字符编码问题

    1.cat /etc/locale.conf LANG="en_US.UTF-8" 2./etc/profile.d/lang.sh这个文件要有且可以正常执行(检查/sbin/co ...

  8. CCFollow和ActionCallFunc

    CCFollow动作,可以让一个节点跟随另一个节点做位移. CCFollow经常用来设置layer跟随sprite,可以实现类似摄像机跟拍的效果 效果是精灵在地图上移动,地图也会跟着移动,但是精灵仍然 ...

  9. Web 服务器被配置为不列出此目录的内容

    在Web.configue文件里,会多出来部分代码,应该是允许浏览目录: <?xml version="1.0" encoding="utf-8"?> ...

  10. Unix系统编程()在文件特定偏移量处的IO:pread和pwrite

    首先我想问的是这两个p代表的是什么? 系统调用pread和pwrite完成与read和write相类似的工作,只是前两者会在offset参数所指定的位置进行文件IO操作,而非始于文件的当前偏移量处,并 ...