回答
從文檔
@屬性(非原子,副本)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
我無法測試它,NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:myString];總是零。 – theWalker
你爲什麼要創建一個可變的屬性字符串?只是:'NSAttributedString * str = [[NSAttributedString alloc] initWithString:@「Some Text」attributes:@ {UITextAttributeTextColor:[UIColor redColor]}];' – rmaddy
乾杯@rmaddy,我使用NSForegroundColorAttributeName。 – DogCoffee
使用
[YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
我在SWIFT使用:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
看來,這適用於其他...我不知道爲什麼它沒有爲我工作過......也許有些項目設置。感謝您的評論。目前我無法再如何測試它。
已過時: 但我不知道爲什麼,文本應用正確,但佔位符顏色保持不變(黑色/灰色)。
--iOS8
這不是一個答案 –
是的,它不是(你的評論)。但是我不想開始一個新的線程,因爲這個代碼與swift中重寫的obj-c代碼是一樣的。 – Tomino
我不知道爲什麼它不適合你。但它對我很好 –
試試這個:
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;
使用代碼標籤! – gsamaras
您可以使用下面的代碼
[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
@ DogCoffee在斯威夫特的答案是
let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
textField.attributedPlaceholder = placeholder
_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個不同的答案。
'_placeholderLabel'的警告:有些用戶報告App Store拒絕:http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color#comment56758204_22017127 – ftvs
此方法適用於沒有任何的子類,沒有任何私人的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
}
}
}
嘗試。
UIColor *color = [UIColor redColor];
self.txtUsername.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Placeholder Text" attributes:@{NSForegroundColorAttributeName:color}];
首先添加此擴展
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
[yourtextFieldName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
這是一個改進版本上面@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 ?? "")
}
}
}
}
- 1. 使用Userattributes更改UITextField的佔位符文本顏色?
- 2. 如何更改UITextField的佔位符顏色?
- 3. UISearchBar更改佔位符顏色
- 4. 更改Braintree佔位符文本顏色
- 5. jQuery更改佔位符文本顏色
- 6. 如何更改輸入佔位符顏色javascript onmouseover,更改佔位符值onmouseover?
- 7. 如何更改Xamarin for iOS7中的UITextField佔位符的顏色和字體
- 8. UITextField更改佔位符顏色,當它變成第一個響應者
- 9. 如何繼承UITextField並覆蓋drawPlaceholderInRect以更改佔位符顏色
- 10. UITextField佔位符字體顏色白色iOS 5?
- 11. 佔位符顏色不會改變
- 12. 如何改變佔位符顏色CSS
- 13. Firefox的佔位符顏色改變
- 14. 更改佔位符的最後一個字符的顏色
- 15. UITextField中佔位符文本的默認顏色是什麼?
- 16. 更改UITextField鍵盤的色調顏色
- 17. 更改內部類佔位符的文本顏色
- 18. 在Javascript中更改佔位符的顏色以適用於Chrome
- 19. 如何更改佔位符文本的顏色
- 20. 如何更改javascript格式佔位符的顏色?
- 21. 如何更改目標c佔位符的顏色
- 22. 在動畫中更改佔位符顏色
- 23. 更改digitalBush/jquery.maskedinput中佔位符的顏色
- 24. CSS - 無法通過分類更改佔位符顏色
- 25. 角度材質2 - 更改md-select佔位符顏色
- 26. 無法更改輸入佔位符的顏色
- 27. 在窗體中更改下拉框佔位符的顏色
- 28. 如何更改默認的select2佔位符顏色的CSS?
- 29. 如何使用javascript更改佔位符的顏色?
- 30. 在驗證文本字段時更改佔位符顏色
在何種情況下,你要改變什麼顏色? – Suryakant
當UITextField被禁用 – theWalker
重複?:[iPhone UITextField - 更改佔位符文本顏色](http://stackoverflow.com/q/1340224/456814)。 – 2014-08-14 01:02:48