UIAlertController消息被截断为一行

时间:2017-11-07 10:08:24

标签: swift user-interface uialertcontroller

在我的应用中,在显示UIAlertController时,它会将消息截断为一行。如何使用自动换行显示全文显示。

screenshot

这是我的代码

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 个答案:

答案 0 :(得分:8)

我发现了这个问题。我在我的代码中覆盖了UILabel类。当我删除代码现在它工作正常。

答案 1 :(得分:1)

从Swift 4开始,您可以使用多行字符串:https://www.hackingwithswift.com/example-code/language/how-to-create-multi-line-string-literals

示例:

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

所以,你的代码是:

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:我的代码使用了很长的短信,如:

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)

什么都没有被截断..还有一些东西搞乱了你的alertController。

Screenshot

答案 2 :(得分:0)

在字符串中,您可以使用" \ n"

手动添加多行
let string = "This is \na string with \nmultiple lines"

应该给你,例如一个总共3行的字符串。

答案 3 :(得分:0)

最近几天我也面临着同样的问题,但终于找到了解决方案。 我删除了“使用UIEdgeInsets填充”代码,它工作正常。

已删除以下代码

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。 希望对您有所帮助!