UITextField placeholder text color
iOS6 and Later 改变UITextField 中占位符 提示文本的文字颜色
在新版本中(iOS6以后)iOS提供一种 Key = value 属性的方式,来改变UI的属性内容。
以UITextField为例
1 | @property ( nonatomic , copy ) NSAttributedString *attributedText NS_AVAILABLE_IOS (6_0); // default is nil |
attributedText 为UITextField 的 public 属性 可以通过键值对的方式来改变 UITextField的内容
例如:
1 2 3 4 5 6 7 8 | if ([textField respondsToSelector: @selector (setAttributedPlaceholder:)] { UIColor*color =[UIColor blackColor]; textField.attributedPlaceholder =[[ NSAttributedString alloc] initWithString:placeholderText attributes:@{ NSForegroundColorAttributeName : color}]; } else { NSLog (@ "Cannot set placeholder text's color, because deployment target is earlier than iOS 6.0" ); // TODO: Add fall-back code to set placeholder color. } |
#iOS6 Earlier
iOS6之前可以重写
1 | -( void )drawPlaceholderInRect:(CGRect)rect; |
例如:
1 2 3 4 5 | -( void ) drawPlaceholderInRect:(CGRect)rect { [[UIColor blueColor] setFill]; [[ self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]]; } |
还有一些其他的方法可以重写控件
当然 你也可以继承UITextField 自定义自己的控件 实现的自定义方法
一般就是 以下几种:
1 2 3 4 5 6 7 8 9 10 | - (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; |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战