Mi requisito es que cuando el marcador de posición del campo de texto esté visible, su color debe ser gris y el tamaño de fuente 10, pero cuando el usuario comienza a escribir en el campo de texto, su color debe ser negro y el tamaño de fuente 14. Probé esto:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!]) textField.textColor = UIColor.black textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)! Pero, el tamaño de fuente de mi marcador de posición está siendo reemplazado por textfield.font , por lo que no puedo obtener un marcador de posición de tamaño 10. ¿Dónde me estoy equivocando? Intenté esto por un par de horas ahora. Cualquier ayuda será apreciada.
Simplemente configure el marcador de posición después de configurar la fuente, ya que la configuración de la fuente también se aplica al marcador de posición (consulte https://developer.apple.com/documentation/uikit/uitextfield/1619604-font ):
textField.font = ... textField.textColor = ... textField.attributedPlaceholder = ...Pruebe este código en su método viewDidLoad() :
textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!]) textField.textColor = UIColor.black textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!Simplemente pruebe este código, tome la salida de acción UITextFieldEditingChanged y utilícelo como se muestra a continuación.
@IBAction func nameTextFieldEditingChanged(_ sender: UITextField) { if sender.text?.isEmpty ?? true { //placeholder text size set here textField.font = UIFont(name: "SourceSansPro-Regular", size: 10)! } else { // When user starting typing textField.textColor = UIColor.black textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)! } }Si tiene alguna duda, por favor comente].