iOS设置TextField的placeholder的颜色,位置,字体,光标颜色
转载自:Z了个Y 简书
一.设置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的类属性
![](http://upload-images.jianshu.io/upload_images/1297992-6d4fadfa716307f4.png?imageMogr2/auto-orient/strip%7CimageView2/2)
使用xib或者storyboard创建
.png
.png
3.实际运行效果图
![](http://upload-images.jianshu.io/upload_images/1297992-4ebbebeb0797469a.png?imageMogr2/auto-orient/strip%7CimageView2/2)
作者:Z了个Y
链接:http://www.jianshu.com/p/db8773b13388
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· Tinyfox 简易教程-1:Hello World!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!