Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UIAlertController消息被截断为一行_Swift_User Interface_Uialertcontroller - Fatal编程技术网

Swift UIAlertController消息被截断为一行

Swift UIAlertController消息被截断为一行,swift,user-interface,uialertcontroller,Swift,User Interface,Uialertcontroller,在我的应用程序中,当显示UIAlertController时,它将消息截断为一行。如何使用换行符显示全文 这是我的密码 let alert = UIAlertController(title: title, message:"Long text" , preferredStyle: UIAlertControllerStyle.alert) let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.defa

在我的应用程序中,当显示
UIAlertController
时,它将消息截断为一行。如何使用换行符显示全文

这是我的密码

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)

在字符串中,可以手动添加多行“\n”


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

由于Swift 4,您可以使用多行字符串:

例如:

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)
乐:我用你的代码发了一条很长的短信,比如:

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还有其他问题


我发现了问题。我在代码中重写UILabel类。当我现在删除代码时,它工作得很好

过去几天我也面临着同样的问题,但最终找到了解决办法。 我删除了“用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。
希望这会有帮助

但我正在显示动态消息,它可能由一行或两行组成。好的,您可以计算字符数,然后在每n个字符后插入一个\n。但这并不是很好,我认为有一个更好的解决方案。是的,我已经这样做了,但如果第n个字符不是空格,它就是在分割单个字符。我不知道为什么UIAlertController行为发生了更改。是的,我没有想到,您可以额外检查空格并在空格后插入\n。在执行URLSessionRequest时,您从何处获得动态消息?来自服务器。你能告诉我怎么做的密码吗。提前谢谢有人问过类似的问题,所以这可能会对你有所帮助:是的,我尝试过使用,最后几个字符被截断@jonsowapple不建议使用长文本提醒。它必须简短易读。请尝试“\n”拆分行是的,我知道了。我显示的消息是动态的,来自服务器。好的,但我使用了您的代码,问题没有出现,没有消息被截断。你必须做些不同的事情。。。见我的编辑上面。谢谢我得到了问题。我正在重写UILabel属性,这就是为什么会发生这种奇怪的行为。我删除了代码,现在它的工作很好,即使我有同样的问题。您覆盖了哪个控制器的UILabel属性?@AnirudhaMahale我已经覆盖了
UILable
类,以添加一些导致此问题的自定义功能。曾经评论说它正在工作fine@Kalikanth040494我怎么能找到它呢?@Hilaj我使用扩展添加了一些额外的自定义函数。我发现了我的问题,因为填充我实际上使用了一个扩展,这使得问题出现在我的案例中
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)
            }
        }
    }
}