[英]Change of UITextField placeholder color
如何動態更改UITextField
占位符顏色? 這始終是相同的系統顏色。
xib編輯器中沒有選項。
來自Docs
@property(非原子,復制)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
斯威夫特4
let str = NSAttributedString(string: "Text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
myTextField.attributedPlaceholder = str
_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}];
PS復制了Stackoverflow的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"];
首先添加此擴展名
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
我在SWIFT中使用它:
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
似乎這適用於其他人...我不知道為什么它之前沒有為我工作...也許一些項目設置。 感謝您的評論。 目前我無法再怎樣測試它。
已過時: 但我不知道為什么,文本應用正確,但占位符顏色保持不變(黑色/灰色)。
--iOS8
試試這個:
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;
您可以使用以下代碼
[txtUsername setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
此解決方案在沒有任何子類化且沒有任何私有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}];
@ DogCoffee在Swift中的回答是
let placeholderAttrs = [ NSForegroundColorAttributeName : UIColor.redColor()]
let placeholder = NSAttributedString(string: "Some text", attributes: placeholderAttrs)
textField.attributedPlaceholder = placeholder
這是@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 ?? "")
}
}
}
}
對於swift 3,我們可以使用此代碼更改UITextfield的占位符文本顏色
let placeholderColor = UIColor.red
mytextField.attributedPlaceholder = NSAttributedString(string: mytextField.placeholder, attributes: [NSForegroundColorAttributeName : placeholderColor])
斯威夫特4
let placeholderColor = UIColor.red
self.passwordTextField?.attributedPlaceholder =
NSAttributedString(string:"placeholderText", attributes:
[NSAttributedStringKey.foregroundColor : placeholderColor])
[yourtextFieldName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.