Word Wrap in UIAlertController in Swift 3

I am creating a quiz app in swift 3 that uses a UITableViewController to list questions and I would like to use an Alert View to post a question with a variety of answers to choose from. I work the way I want with one giant quirk and that is that long responses get a much smaller font and are truncated by replacing the text in the middle of the response with ellipsis.

How do I get word wrapping responses in AlertView actions and keep the font the same size? Note that manually inserting "\ n" in responses after a certain character length is not realistic as there are hundreds of questions in the question dictionary.

enter image description here

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

      

+3


source to share


1 answer


try it

  UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0 // change your self what you need

      

eg,



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

enter image description here

+4


source







All Articles