When user presses a button in my app I show him the alert view with an input text field there. I use the following code for that:
func addTapped(sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "", message: "Write a #name of a tag that you want to follow", preferredStyle: .Alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .Default) { (_) in
if let field = alertController.textFields![0] as? UITextField {
if(field.text?.characters.count > 0) {
print("user put some data inside")
} else {
print("user did not put anything")
}
} else {
// user did not fill field
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in }
alertController.addTextFieldWithConfigurationHandler { (textField) in
let attributedString = NSMutableAttributedString(string: "C-TAD-")
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: NSMakeRange(0,6))
textField.attributedText = attributedString
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
It works well, when user runs the app he sees:
but this placeholder text inside is removable. I would like to prevent that and always show this text there, so basically lock the delete option on it.
How can I lock the possibility of removing those first letters?