Come modificare UITextfield
placeholder
& fontsize
in SWIFT 2.0?Come modificare il colore del segnaposto UITextfield e il fontsize usando swift 2.0?
risposta
PlaceHolder TextFiled Swift 3.0
# 1. set Segnaposto colore campo di testo a livello di programmazione
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count)) // Color
txtTitle.attributedPlaceholder = myMutableStringTitle
O
txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])
Nota: Name
è il tuo segnaposto di textField
.
PlaceHolder TextFiled:
------------------------------- - O -------------------------------------
# 2. set Segnaposto colore campo di testo in fase di esecuzione attributo
La seconda soluzione era la migliore per me, volevo raggruppare tutti i campi in un 'IBOutletCollection', tuttavia non ero sicuro di come definire ogni UITextfields stringhe segnaposto. In questo modo funziona perfettamente con Swift 3. –
Si può provare con questo codice di esempio
let textFld = UITextField();
textFld.frame = CGRectMake(0,0, 200, 30)
textFld.center = self.view.center;
textFld.attributedPlaceholder = NSAttributedString(string:"Test Data for place holder", attributes:[NSForegroundColorAttributeName: UIColor.blueColor(),NSFontAttributeName :UIFont(name: "Arial", size: 10)!])
self.view.addSubview(textFld)
aggiornato per Swift 3
Se si desidera cambiare la colore UITextField Segnaposto per Swift 3, utilizzare le seguenti righe di codice:
let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])
http://stackoverflow.com/a/26076202/988169 – pkc456
Eventuali duplicati di [Modifica Segnaposto Colore Testo Swift] (http: // stackoverflow.com/questions/26076054/changing-placeholder-text-color-with-swift) – Moritz