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

Nat*_*jan 7 objective-c placeholder uitextfield ios

我试图将UITextField "占位符"颜色设置为黑暗.

NSAttributedString * search = [[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

textField.attributedPlaceholder = search;
Run Code Online (Sandbox Code Playgroud)
  • 但仍然UITextField"占位符"中显示浅灰色.

  • 是否可以设置深色"占位符"颜色UITextField

我也尝试了另一种方法

[textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

但这两种方法都适用于iOS 7,但不适用于iOS 6.

  • 是否可以在iOS 6目标中设置黑暗的"占位符"颜色?UITextField

谢谢!

Pab*_*meu 20

正如Apple所建议的那样,继承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]; 
    } 
 } 
Run Code Online (Sandbox Code Playgroud)

图片来源:http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6


Kum*_* KL 9

覆盖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];
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

如果内部变量将来发生变化,这种情况可能会崩溃

编程方式:

  [self.MyTextField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

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]; 

        }  
Run Code Online (Sandbox Code Playgroud)


gam*_*r87 8

对于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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*han 7

它非常简单....

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

UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
Run Code Online (Sandbox Code Playgroud)