【问题标题】:UITextField set placeholder color as dark in iOSUITextField 在 iOS 中将占位符颜色设置为深色
【发布时间】:2014-06-03 15:39:27
【问题描述】:

我已尝试将 UITextField "Placeholder" 颜色设置为深色。

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 设置深色 "placeholder" 颜色?

谢谢!

【问题讨论】:

  • 您发布的代码应该可以工作 - 它对我有用。确保在此之后没有设置常规 placeholder 属性的调用。调用代码时还要确保textField 不是nil

标签: ios objective-c uitextfield placeholder


【解决方案1】:

覆盖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];
    }
}

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

以编程方式:

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

        }  

【讨论】:

  • 这个功能会在功能上有所改变吗?我的意思是,自定义 UITextField 是否有效?
  • 高效的方式是什么意思?
  • 使用这种方法不是一个好主意。它访问文本字段的私有子视图结构。它可能会在以后的任何 iOS 更新中失败。
  • 那么,将 UITextField 占位符颜色更新为深色的有效方法是什么? @rmaddy
  • @rmaddy : 你能不能请展开它它访问文本字段的私有子视图结构..即使我不确定所以..
【解决方案2】:

很简单....

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

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

【讨论】:

  • 为什么对 iOS 6 使用不同的方法? iOS 6 支持attributePlaceholder 属性。
  • 正如我对另一个答案的评论,方法 2 将在未来的 iOS 版本中实现更改后立即开始崩溃您的应用程序。
【解决方案3】:

试试这个

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

【讨论】:

  • 这会导致 iOS 13 出现异常:*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug'
【解决方案4】:

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

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

【讨论】:

  • 嗨,我想使用 iOS 功能作为 textField.attributedPlaceholder。但是这种方法在 iOS 7 上有效,但在 iOS 6 上无效。想法?
  • 我明白,但是例如更改占位符的文本颜色不是“iOS 功能”,这是我建议改用自定义 UILabel 的原因。这保证适用于 iOS 6/7/8+。
【解决方案5】:

它将改变textField的占位符文本颜色

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

【讨论】:

    【解决方案6】:

    按照 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

    【讨论】:

    • 也适用于 iOS8。
    【解决方案7】:

    对于 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)
                }
            }
        }
    }
    

    【讨论】:

    • 这太酷了...以前从未听说过@IBDesignable@IBInspectable,谢谢!
    【解决方案8】:

    这是更改占位符文本的字体和颜色的最新方法

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

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2020-01-09
      • 2014-01-31
      • 2019-01-03
      • 1970-01-01
      • 2017-05-01
      相关资源
      最近更新 更多