UIAlertController message getting truncated to one line

丶灬走出姿态 提交于 2019-12-01 08:09:12

问题


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.

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)

回答1:


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




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/47155129/uialertcontroller-message-getting-truncated-to-one-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!