UITextField在iOS中将占位符颜色设置为黑暗

我曾尝试将UITextField “占位符”颜色设置为黑暗。

 NSAttributedString * search = [[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}]; textField.attributedPlaceholder = search; 
  • 但仍然在“占位符”中显示浅灰色的UITextField

  • 是否有可能为UITextField设置黑暗的“占位符”颜色?

另外我也尝试了另一种方法

 [textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"]; 

但是这两种方法都在iOS 7中工作,但不能在iOS 6上工作。

  • 是否有可能在iOS 6目标UITextField设置黑暗的“占位符”颜色?

谢谢!

正如苹果所build议的,UITextField的子类化和覆盖- (void)drawPlaceholderInRect:(CGRect)rect是要走的路:

 - (void)drawPlaceholderInRect:(CGRect)rect { UIColor *colour = [UIColor lightGrayColor]; if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) { // iOS7 and later NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font}; CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil]; [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; } else { // iOS 6 [colour setFill]; [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment]; } } 

信用: http : //www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6

重写drawPlaceholderInRect:方法来绘制我们自己的占位符文本。

 - (void)drawPlaceholderInRect:(CGRect)rect { UIColor *colour = [UIColor darkColor]; if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) { // iOS7 and later NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font}; CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil]; [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; } else { // iOS 6 [colour setFill]; [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment]; } } 

要么

这种情况下,如果内部variables在未来发生变化,可能会崩溃

编程方式:

  [self.MyTextField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"]; 

UIStoryBoard

在这里输入图像说明

要么

编辑:玩UITextFiled委托。 它像一个棘手的:

 -(void)viewDidLoad{ self.mytxtField.text = @"PlaceholderText"; self.mytxtField.delegate = self; self.mytxtField.textColor = [UIColor darkGrayColor]; } -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ if ([self.mytxtField.text isEqualToString: @"PlaceholderText"]) { self.mytxtField.text = @""; self.mytxtField.textColor = [UIColor blackColor]; } } -(void)textFieldDidEndEditing:(UITextField *)textField{ if ([self.mytxtField.text length]<1) { self.mytxtField.text =@"PlaceholderText"; self.mytxtField.textColor = [UIColor darkGrayColor]; } 

对于iOS8,您可以利用@IBDesignable@IBInspectable

这是Swift中的UITextField子类,它允许您在Interface Builder中设置占位符颜色,并立即查看结果:

 @IBDesignable class EDTextField: UITextField { @IBInspectable var placeholderColor: UIColor = UIColor.whiteColor() { didSet { if let placeholder = self.placeholder { let attributes = [NSForegroundColorAttributeName: placeholderColor] attributedPlaceholder = NSAttributedString(string: placeholder, attributes: attributes) } } } } 

它非常简单….

试试这个改变占位符的文字颜色..

 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}]; 

尝试这个

  [textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"]; 

为了使其具有面向未来和完全可定制的特性(颜色,字体,大小等),我只需在顶部添加一个UILabel ,然后根据UITextField内容手动隐藏/显示它。

只要确保将标签的userInteractionEnabled设置为NO

它将改变textField的palceholder textColor

 [yourTextField setValue:[UIColor colorWithRed:150.0f/255.0f green:150.0f/255.0f blue:155.0f/255.0f alpha:1] forKeyPath:@"_placeholderLabel.textColor"]; 

这里是最新的方式来改变占位符文本的字体和颜色

  self.emailField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Email" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName :[UIFont fontWithName:@"OpenSans" size:14.0]}];