How can I make a multiline editable text UITextview
inside a UIAlertController
? UITextfield
supports a single line and we need to make
I made a few tweaks to Vishal's approach.
Set the textView border width, border color and background color to make it visible.
class ViewController: UIViewController, UITextViewDelegate {
...
@IBAction func showAlert(sender: AnyObject) {
let alertController = UIAlertController(title: "Hello, I'm alert! \n\n\n\n\n\n\n", message: "", preferredStyle: .Alert)
let rect = CGRectMake(15, 50, 240, 150.0)
let textView = UITextView(frame: rect)
textView.font = UIFont(name: "Helvetica", size: 15)
textView.textColor = UIColor.lightGrayColor()
textView.backgroundColor = UIColor.whiteColor()
textView.layer.borderColor = UIColor.lightGrayColor().CGColor
textView.layer.borderWidth = 1.0
textView.text = "Enter message here"
textView.delegate = self
alertController.view.addSubview(textView)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let action = UIAlertAction(title: "Ok", style: .Default, handler: { action in
let msg = (textView.textColor == UIColor.lightGrayColor()) ? "" : textView.text
print(msg)
})
alertController.addAction(cancel)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: {})
}
func textViewDidBeginEditing(textView: UITextView) {
if textView.textColor == UIColor.lightGrayColor(){
textView.text = ""
textView.textColor = UIColor.darkGrayColor()
}
}
}