2015-10-29 29 views

risposta

40

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:

enter image description here

------------------------------- - O -------------------------------------

# 2. set Segnaposto colore campo di testo in fase di esecuzione attributo

  1. Set textfield testo segnaposto Enter Title

    enter image description here

  2. Clicca sull'identità ispettore della proprietà campo di testo.Definire

    enter image description here

  3. attributi utente Runtime, aggiungere gli attributi di colore

    Percorso chiave: _placeholderLabel.textColor

    Tipo: Color

    valore: Your Color or RGB value

    enter image description here

    PlaceHolder TextFiled:

    enter image description here

+0

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. –

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) 
8

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]) 
Problemi correlati