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

时间:2014-04-19 04:24:34

标签: ios objective-c uitextfield placeholder

我试图将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设置深色“占位符”颜色?

谢谢!

8 个答案:

答案 0 :(得分: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]; 
    } 
 } 

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

答案 1 :(得分: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];
    }
}

<强> OR

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

编程:

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

UIStoryBoard

enter image description here

修改 与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]; 

        }  

答案 2 :(得分:8)

对于iOS8,您可以利用@IBDesignable&amp; @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)
            }
        }
    }
}

答案 3 :(得分:7)

非常简单......

尝试此更改占位符文字颜色..

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

答案 4 :(得分:1)

试试这个

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

答案 5 :(得分:1)

以下是更改占位符文本的FONT和COLOR的最新方法

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

答案 6 :(得分:0)

为了使其面向未来且完全可自定义(颜色,字体,大小等),我只需在顶部添加UILabel并根据UITextField内容手动隐藏/显示它

只需确保将标签userInteractionEnabled设置为NO

答案 7 :(得分:0)

它将更改textField的掌柜textColor

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