UITextField的placeholder颜色的修改

  • 使用属性
@property(nonatomic,copy)   NSAttributedString     *attributedPlaceholder;

// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];

// NSAttributedString : 带有属性的文字(富文本技术)
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attrs];
self.phoneField.attributedPlaceholder = placeholder;

NSMutableAttributedString *placehoder = [[NSMutableAttributedString alloc] initWithString:@"手机号"];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, 1)];
[placehoder setAttributes:@{
                            NSForegroundColorAttributeName : [UIColor yellowColor],
                            NSFontAttributeName : [UIFont systemFontOfSize:30]
                            } range:NSMakeRange(1, 1)];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(2, 1)];
self.phoneField.attributedPlaceholder = placehoder;
  • 重写方法
- (void)drawPlaceholderInRect:(CGRect)rect
{
    [self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, 25) withAttributes:@{
                                                       NSForegroundColorAttributeName : [UIColor grayColor],
                                                       NSFontAttributeName : self.font}];
}
  • 使用KVC
[self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
时间: 2024-11-09 20:21:54

UITextField的placeholder颜色的修改的相关文章

【代码笔记】UITextField设置placeholder颜色

一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITextFieldDelegate> @end RootViewController.m #import "RootViewController.h" @interface RootViewController () @end @i

IOS 开发更改UITextField的Placeholder颜色

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];    UIColor *color = [UIColor redColor]; textField.backgroundColor = [UIColor yellowColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initW

iOS UITextField设置placeholder颜色

设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName: color}]; iOS UITextField设置placeholder颜色

UITextField自定义placeHolder颜色并保持placeHolder居中

思路: 1.自定义UITextField的子类 2.重写drawPlaceholderInRect方法改变placeHolder颜色 3.重写placeholderRectForBounds方法保持placeHolder文字居中 代码如下: @interface CustomPlaceHolderTextField : UITextField @end @implementation CustomPlaceHolderTextField - (void)drawPlaceholderInRect

修改HTML5 input placeholder 颜色及修改失效的解决办法

input::input-placeholder{color: #bdbdbd ;} /* 有些资料显示需要写,有些显示不需要,但是在编辑器webstorm中该属性不被识别 */ ::-webkit-input-placeholder { /* WebKit browsers */ color: #999; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #999; } ::-moz-placeholder { /* Mozi

改变UITextField的Placeholder颜色

通过 attributedPlaceholder 属性来改变 if([textField respondsToSelector:@selector(setAttributedPlaceholder:)] { UIColor*color =[UIColor blackColor]; textField.attributedPlaceholder =[[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSFo

iOS修改UITextField的Placeholder字体颜色

修改UITextField的Placeholder字体颜色有一下两张方式可以达到效果. 第一种: UIColor *color = [UIColor whiteColor]; textfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName: color}]; 第二种: [textfield set

iOS利用storyboard修改UITextField的placeholder文字颜色

最近有个需求需要修改UITextField的placeholder文字颜色,在网上找发现有用代码修改的,但是考虑到更加优雅的实现,所以尝试着在storyboard中直接实现,结果竟然真的成功了(原谅我太小白),实现的位置如下: 具体步骤: 1.在User Defined Runtime Attributes中添加一个Key. 2.输入Key Path(这里我们输入_placeholderLabel.textColor). 3.选择Type,有很多种(这里我们选择Color) 4.设置Value(

修改 UITextfield placeholder 颜色

_password = [[UITextField alloc]init]; _password.font = k18Font; _password.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@" 密码" attributes:@{NSForegroundColorAttributeName:kBlackColor}]; ***************** 而且 UITextField 比 UIL