UIAlertController message getting truncated to one line

Viewed 4965

In my app, while presenting the UIAlertController it's truncating the message to one line. How to make it display to show full text with word wrap.

screenshot

Here is my code

let alert = UIAlertController(title: title, message:"Long text"  , 
preferredStyle: UIAlertControllerStyle.alert) 

let okAction = UIAlertAction(title: "OK", style: 
UIAlertActionStyle.default, handler: nil)  

alert.addAction(okAction)

self.present(alert, animated: true, completion: nil)
4 Answers

I found the issue.I was overriding the UILabel class in my code. when i have removed the code now it's working fine.

I was also facing the same problem from the last few days but finally found the solution. I removed "padding with UIEdgeInsets" code, it's working fine.

Removed below code

extension UILabel {

    public var padding: UIEdgeInsets? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.padding) as? UIEdgeInsets
        }
        set {
            if let newValue = newValue {
                objc_setAssociatedObject(self, &AssociatedKeys.padding, newValue as UIEdgeInsets, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
}

UIAlertviewController. Hope it will help!!!

Since Swift 4 you can use multi-line strings: https://www.hackingwithswift.com/example-code/language/how-to-create-multi-line-string-literals

Example:

let longString = """
When you write a string that spans multiple
lines make sure you start its content on a
line all of its own, and end it with three
quotes also on a line of their own.
Multi-line strings also let you write "quote marks"
freely inside your strings, which is great!
"""

So, your code would be:

let longTextMessage = """
When you write a string that spans multiple
lines make sure you start its content on a
line all of its own, and end it with three
quotes also on a line of their own.
Multi-line strings also let you write "quote marks"
freely inside your strings, which is great!
"""

let alert = UIAlertController(title: title, message:longTextMessage, preferredStyle: UIAlertControllerStyle.alert) 

let okAction = UIAlertAction(title: "OK", style: 
UIAlertActionStyle.default, handler: nil)  

alert.addAction(okAction)

self.present(alert, animated: true, completion: nil)

LE: I used your code with a long text message like:

let alert = UIAlertController(title: title, message:"Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text ",
                                      preferredStyle: UIAlertControllerStyle.alert)

        let okAction = UIAlertAction(title: "OK", style:
            UIAlertActionStyle.default, handler: nil)

        alert.addAction(okAction)

        self.present(alert, animated: true, completion: nil)

Nothing gets truncated.. there's something else that is messing with your alertController.

Screenshot

In your string, you can manually add multiple lines with "\n"

let string = "This is \na string with \nmultiple lines"

should give you e.g. an string with total 3 lines.

Related