2014-01-12 239 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 ?? "") 
     } 
    } 
} 

}

相關問題