2017-12-26 5 views
0

Ich habe benutzerdefinierte textField, die @IBInspectable Eigenschaft placeHolderColor: UIColor hat und es funktioniert gut. Ich stelle es durch:UITextField attributiedPlaceholder Farbe und Opazität in Swift 4

attributedPlaceholder = NSAttributedString(string: placeHolder, attributes:[NSAttributedStringKey.foregroundColor: placeHolderColor]) 

Wie kann ich nur für diese Eigenschaft programmatisch einen Opazität Wert gesetzt, nicht für die normalen Text in meinem Textfeld? Ich habe keine passenden NSAttributedStringKey gefunden, um dies zu tun

Antwort

0

UIColor Klassenmethoden withAlphaComponent(alpha:), um eine Farbe Alpha einzustellen. read more

@IBInspectable var placeholderTextColor: UIColor? { 
    set { 
     guard let color = newValue else { return } 

     let placeholderText = self.placeholder ?? "" 
     attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedStringKey.foregroundColor: color.withAlphaComponent(alpha: self.alpha)]) 
    } 
    get{ 
     return self.placeholderTextColor 
    } 
} 
Verwandte Themen