iOS设置TextField的placeholder的颜色,位置,字体,光标颜色
2017-02-15 17:33
645 查看
前言
由于项目需要修改TextField的占位符的颜色,位置等,总结下如何设置UITextField的占位符的一些属性。
一.设置placeholder的颜色字体
1.iOS6.0之后苹果提供了attributedPlaceholder属性可以设置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"];
二.设置placeholder的文本的位置
苹果给我们提供了以下方法可以自定义一个TextField,我们可以重写这些方法定制自己的UITextField。// 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文件
#import <UIKit/UIKit.h> @interface ZYTextField : UITextField @end
.m文件
#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.纯代码创建UITextField
UITextField *textField = [[ZYTextField alloc]initWithFrame:CGRectMake(0, 300, 300, 35)]; textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:textField];
b.使用xib或者storyboard创建
修改UITextField的类属性
使用xib或者storyboard创建
.png
3.实际运行效果图
运行效果.png
相关文章推荐
- 关于iOS 类扩展Extension的进一步深层理解
- IOS:Warning: Attempt to dismiss from view controller <xxxxxxxxxController: 0x15bd67f0> while a prese
- iOS 推送全解析,你不可不知的所有 Tips!
- iOS 获取当前正在显示的ViewController
- iOS 对象之间的通信方式(delegate、block、NSNotification、kvc、kvo)总结
- iOS Universal Links(通用链接)
- iOS开发经验总结3
- iOS开发经验总结2
- iOS开发经验总结1
- ios 开发 ping
- iOS设计模式-策略模式
- iOS导航栏的隐藏方式
- 四,iOS 界面中的摇一摇功能实现
- iOS 开发中的几种设计模式
- 关于iphone accessory的理解
- IOS开发NSBundle对象使用详解
- iOS学习-删除storyboard中viewController黑屏问题
- ios常见加密解密方法(RSA、DES 、AES、MD5)
- iOS开发 开发者账户介绍
- 安装qemu-2.0.0+dfsg出现××/pci-bios/bios.bin’: No such file or directory 的问题