【问题标题】:Change of UITextField placeholder colorUITextField 占位符颜色的变化
【发布时间】:2014-01-31 04:52:15
【问题描述】:

如何动态更改UITextField 的占位符颜色? 这始终是相同的系统颜色。

xib 编辑器中没有选项。

【问题讨论】:

标签: ios uitextfield


【解决方案1】:

来自文档

@property(nonatomic, copy) NSAttributedString *attributedPlaceholder

此属性默认为 nil。如果设置,占位符字符串是 使用 70% 的灰色和剩余的样式信息绘制 属性字符串的(文本颜色除外)。分配一个新的 此属性的值也会替换占位符的值 具有相同字符串数据的属性,尽管没有任何格式 信息。为该属性分配新值不会影响 文本字段的任何其他与样式相关的属性。

Objective-C

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }];
self.myTextField.attributedPlaceholder = str;

斯威夫特

let str = NSAttributedString(string: "Text", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
myTextField.attributedPlaceholder = str

斯威夫特 4

let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str

【讨论】:

  • 我无法测试,NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:myString];始终为零。
  • 为什么要创建可变属性字符串?就这样做:NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Some Text" attributes:@{ UITextAttributeTextColor : [UIColor redColor] }];
  • 干杯@rmaddy,我使用了NSForegroundColorAttributeName。
【解决方案2】:

_placeholderLabel.textColor

迅速

myTextField.attributedPlaceholder = 
NSAttributedString(string: "placeholder", attributes:[NSForegroundColorAttributeName : UIColor.redColor()])

Objective-C

UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
   [[NSAttributedString alloc]
   initWithString:@"Full Name"
   attributes:@{NSForegroundColorAttributeName:color}];

P.S 从 Stackoverflow 复制了 3 个不同的答案。

【讨论】:

  • 警告_placeholderLabel:部分用户报告App Store拒绝:stackoverflow.com/questions/1340224/…
  • 如果你使用了 _placeholderLabel,你会得到这个崩溃 禁止访问 UITextField 的 _placeholderLabel ivar。这是一个应用程序错误
【解决方案3】:

使用下面的代码

[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];

【讨论】:

  • 如果您使用它,您的应用将被拒绝,您正在访问私有 API。而是使用 UITextField 的属性占位符属性。
  • 我认为这不是您可以在当前应用程序中使用的私有 API。不使用任何私有 api
  • @aumanets 它不是私有 api...它是 UITextfield 的私有属性..它是键值编码...检查此stackoverflow.com/questions/16601468/…
  • 是的,你是对的,它是私有财产。无论如何,以这种方式访问​​属性/变量不是一个好习惯。有一天,Apple 可以将该属性的名称更改为会使应用程序崩溃的其他名称,并且您的代码中不会收到任何编译警告。
  • 如果你使用了 _placeholderLabel,你会得到这个崩溃 禁止访问 UITextField 的 _placeholderLabel ivar。这是一个应用程序错误
【解决方案4】:

先添加这个扩展

extension UITextField{
    @IBInspectable var placeHolderTextColor: UIColor? {
        set {
            let placeholderText = self.placeholder != nil ? self.placeholder! : ""
            attributedPlaceholder = NSAttributedString(string:placeholderText, attributes:[NSForegroundColorAttributeName: newValue!])
        }
        get{
            return self.placeHolderTextColor
        }
    }
}

然后您可以通过情节提要更改占位符文本颜色,也可以像这样设置它:

textfield.placeHolderTextColor = UIColor.red

【讨论】:

    【解决方案5】:

    我在 SWIFT 中使用它:

    myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])

    似乎这对其他人有用......我不知道为什么它以前不适合我......也许是一些项目设置。感谢cmets。目前我没有办法再次测试它。

    已过时: 但我不知道为什么,文本应用正确,但占位符颜色保持不变(黑色/灰色)。

    --iOS8

    【讨论】:

    • 是的,它不是(也不是你的评论)。但我不想开始一个新线程,因为这段代码只是用 swift 重写的相同的 obj-c 代码。
    • 我不知道为什么它不适合你。但它对我来说很好
    • 我也不知道,但这似乎适用于其他人:-) 也许一些项目设置......
    【解决方案6】:

    试试这个:

    NSAttributedString *strUser = [[NSAttributedString alloc] initWithString:@"Username" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
    NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
    
    self.username.attributedPlaceholder = strUser;
    self.password.attributedPlaceholder = strPassword;
    

    【讨论】:

      【解决方案7】:

      您可以使用以下代码

      [txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
      

      【讨论】:

        【解决方案8】:

        此解决方案无需任何子类化且无需任何私有 ivars 即可工作:

        @IBOutlet weak var emailTextField: UITextField! {
            didSet {
                if emailTextField != nil {
                    let placeholderText = NSLocalizedString("Tap here to enter", comment: "Tap here to enter")
                    let placeholderString = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor(white: 0.66, alpha: 1.0)])
                    emailTextField.attributedPlaceholder = placeholderString
                }
            }
        }
        

        【讨论】:

          【解决方案9】:

          试试这个。

          UIColor *color = [UIColor redColor];
          self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}];
          

          【讨论】:

            【解决方案10】:

            @DogCoffee 在 Swift 中的回答是

            let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
            let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
            
            textField.attributedPlaceholder = placeholder
            

            【讨论】:

              【解决方案11】:

              这是上面@Medin Piranej 提供的扩展的改进版本(顺便说一句好主意!)。如果您尝试获取 placeHolderTextColor,此版本可避免无限循环,并在颜色集为 nil 时防止崩溃。

              public extension UITextField {
              
              @IBInspectable public var placeholderColor: UIColor? {
                  get {
                      if let attributedPlaceholder = attributedPlaceholder, attributedPlaceholder.length > 0 {
                          var attributes = attributedPlaceholder.attributes(at: 0,
                                                                            longestEffectiveRange: nil,
                                                                            in: NSRange(location: 0, length: attributedPlaceholder.length))
                          return attributes[NSForegroundColorAttributeName] as? UIColor
                      }
                      return nil
                  }
                  set {
                      if let placeholderColor = newValue {
                          attributedPlaceholder = NSAttributedString(string: placeholder ?? "",
                                                                     attributes:[NSForegroundColorAttributeName: placeholderColor])
                      } else {
                          // The placeholder string is drawn using a system-defined color.
                          attributedPlaceholder = NSAttributedString(string: placeholder ?? "")
                      }
                  }
              }
              

              }

              【讨论】:

                【解决方案12】:

                对于 swift 3,我们可以使用此代码更改 UITextfield 的占位符文本颜色

                 let placeholderColor = UIColor.red
                 mytextField.attributedPlaceholder = NSAttributedString(string: mytextField.placeholder, attributes: [NSForegroundColorAttributeName : placeholderColor])
                

                【讨论】:

                  【解决方案13】:

                  斯威夫特 4

                  let placeholderColor = UIColor.red
                  self.passwordTextField?.attributedPlaceholder = 
                  NSAttributedString(string:"placeholderText", attributes: 
                  [NSAttributedStringKey.foregroundColor : placeholderColor])
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2013-09-12
                    • 2012-04-12
                    • 2012-05-16
                    • 2010-11-23
                    • 1970-01-01
                    • 2019-04-04
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多