I am building a quiz app in swift 3 which leverages a UITableViewController to list the questions, and what I would like to use is a Alert View to post the question with a variety of answers to choose from. I have it working the way I want it to with one giant quirk, and that is that the long answers get a much smaller font, and are truncated by swapping out text in the middle of the answer with an ellipsis.
How do I get the answers to word wrap in the AlertView actions, and maintain the same size font? Note that manually inserting "\n" in the answers after a certain character length is not realistic because there are hundreds of questions in the question dictionary.

This is the code for the AlertView:
func showQuestion(questionNum: Int) {
        let alertTitle = "Question \(questionNum + 1)"
        let qDict = test[questionNum] as! [String:String]
        let alertMessage = qDict["Question"]
        // create the alert
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
        // add the actions (buttons)
        if qDict["Option-A"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-A"], style: UIAlertActionStyle.default, handler: nil))
        }
        if qDict["Option-B"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-B"], style: UIAlertActionStyle.default, handler: nil))
        }
        if qDict["Option-C"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-C"], style: UIAlertActionStyle.default, handler: nil))
        }
        if qDict["Option-D"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-D"], style: UIAlertActionStyle.default, handler: nil))
        }
        if qDict["Option-E"] != "" {
            alert.addAction(UIAlertAction(title: qDict["Option-E"], style: UIAlertActionStyle.default, handler: nil))
        }
        alert.addAction(UIAlertAction(title: "Come back to this one", style: UIAlertActionStyle.destructive, handler: nil))
        // show the alert
        self.present(alert, animated: true, completion: nil)
    }
                try this
  UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0 // change your self what you need
for e.g
let alert = UIAlertController(title: title,
                                  message: "dsfdsf",
                                  preferredStyle: UIAlertControllerStyle.alert)
    let cancelAction = UIAlertAction(title: "karthik tested for the word wrap text in alert",
                                     style: .cancel, handler: nil)
    alert.addAction(cancelAction)
    self.present(alert, animated: true, completion: nil)
      // number of lines
      UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0
      // for font
      UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).font = UIFont.systemFont(ofSize: 10.0)
output

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With