Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController message getting truncated to one line

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)
like image 391
Kalikanth040494 Avatar asked Nov 07 '17 10:11

Kalikanth040494


3 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.

like image 83
Kalikanth040494 Avatar answered Sep 30 '22 16:09

Kalikanth040494


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!!!

like image 23
Ravi Nadendla Avatar answered Sep 30 '22 15:09

Ravi Nadendla


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

like image 32
Mihai Erős Avatar answered Sep 30 '22 15:09

Mihai Erős