2014-01-12 240 views
73

如何动态更改UITextField的占位符颜色? 这总是一样的系统颜色。更改UITextField占位符颜色

在xib编辑器中没有选项。

+0

在何种情况下,你要改变什么颜色? – Suryakant

+0

当UITextField被禁用 – theWalker

+1

重复?:[iPhone UITextField - 更改占位符文本颜色](http://stackoverflow.com/q/1340224/456814)。 – 2014-08-14 01:02:48

回答

161

从文档

@属性(非原子,副本)NSAttributedString * attributedPlaceholder

此属性默认为零。如果设置,占位符字符串是 ,使用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 
+0

我无法测试它,NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:myString];总是零。 – theWalker

+0

你为什么要创建一个可变的属性字符串?只是:'NSAttributedString * str = [[NSAttributedString alloc] initWithString:@“Some Text”attributes:@ {UITextAttributeTextColor:[UIColor redColor]}];' – rmaddy

+0

干杯@rmaddy,我使用NSForegroundColorAttributeName。 – DogCoffee

12

使用

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

如果您使用此应用程序,您的应用程序将被拒绝,您正在访问私有API。而是使用UITextField的属性PropertyPlaceholder属性。 – aumanets

+0

我认为这不是您可以在当前应用中使用的私人API。不使用任何私人api –

+0

它是私人API,您正在访问私有变量“_placeholderLabel”。如果你可以做一些像self.placeholderLabel这样的东西,它就不是私人的。 – aumanets

3

我在SWIFT使用:

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

看来,这适用于其他...我不知道为什么它没有为我工作过......也许有些项目设置。感谢您的评论。目前我无法再如何测试它。

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

--iOS8

+2

这不是一个答案 –

+0

是的,它不是(你的评论)。但是我不想开始一个新的线程,因为这个代码与swift中重写的obj-c代码是一样的。 – Tomino

+0

我不知道为什么它不适合你。但它对我很好 –

2

试试这个:

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; 
+0

使用代码标签! – gsamaras

1

您可以使用下面的代码

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

@ DogCoffee在斯威夫特的答案是

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

textField.attributedPlaceholder = placeholder 
41

Easy and perfect solution.

_placeholderLabel.textColor 

在迅速

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

目标C

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

P.S复制从#1 3个不同的答案。

+1

'_placeholderLabel'的警告:有些用户报告App Store拒绝:http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color#comment56758204_22017127 – ftvs

1

此方法适用于没有任何的子类,没有任何私人的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 
     } 
    } 
} 
1

尝试。

UIColor *color = [UIColor redColor]; 
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}]; 
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 
-2
[yourtextFieldName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"]; 
0

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

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 ?? "") 
     } 
    } 
} 

}

相关问题