iOS中如何设置UITextField placeholder 的颜色和距离

最近有一个需求: 需要改变UITextField 的 placeholder的颜色和字体大小,我找了一些方法 总结一下:

1.使用富文本 (attributedPlaceholder)

这种方法的缺点是:如果设置placeholder的字体比文字字体小的时候,placeholder的文字会偏上。

UITextField *tf2 = [[UITextField alloc] init];
tf2.frame = CGRectMake(75, 160, 200, 40);
tf2.borderStyle = UITextBorderStyleRoundedRect;
tf1.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"attributedPlaceholder" attributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:13]}];
[self.view addSubview:tf2];

2.KVC修改Placeholder

UITextField *tf3 = [[UITextField alloc] init];
tf3.frame = CGRectMake(75, 220, 200, 40);
tf3.borderStyle = UITextBorderStyleRoundedRect;
tf3.placeholder = @"KVC修改Placeholder";
[tf3 setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[tf3 setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
[self.view addSubview:tf3];

3.重写drawPlaceholderInRect

  • JYTextField *tf4 = [[JYTextField alloc] init];
    tf4.frame = CGRectMake(75, 280, 200, 40);
    tf4.borderStyle = UITextBorderStyleRoundedRect;
    tf4.placeholder = @"重写drawPlaceholderInRect";
    [self.view addSubview:tf4];
  • (void)drawPlaceholderInRect:(CGRect)rect
    {
    UIColor *placeholderColor = [UIColor redColor];//设置颜色
    [placeholderColor setFill];
    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);//设置距离
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    style.alignment = self.textAlignment;
    NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, [UIFont systemFontOfSize:13], NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];
    [self.placeholder drawInRect:placeholderRect withAttributes:attr];
    }

4.UITextField 上添加一个label

通过 监测UITextFiled 的状态,来判断label 的显示与隐藏。这个特别要注意通知的使用。

UITextField *tf5 = [[UITextField alloc] init];
self.tf5 = tf5;
tf5.delegate = self;
tf5.frame = CGRectMake(75, 340, 200, 40);
tf5.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:tf5];

UILabel *tf5Placeholder = [[UILabel alloc] init];
self.tf5Placeholder = tf5Placeholder;
tf5Placeholder.frame = CGRectMake(10, 0, 200, 40);
tf5Placeholder.textColor = [UIColor lightGrayColor];
tf5Placeholder.text = @"label替换";
tf5Placeholder.font = [UIFont systemFontOfSize:14];
[tf5 addSubview:tf5Placeholder];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:) name:UITextFieldTextDidChangeNotification object:nil];

-(void)textFiledEditChanged:(NSNotification *)obj{
if ([self.tf5.text length] >0) {
self.tf5Placeholder.hidden = YES;
}else {
self.tf5Placeholder.hidden = NO;
}
}

-- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

最后附上Demo 地址稍后附上

你可能感兴趣的:(iOS中如何设置UITextField placeholder 的颜色和距离)