【问题标题】:UIAlertController message getting truncated to one lineUIAlertController 消息被截断为一行
【发布时间】:2017-11-07 10:08:23
【问题描述】:

在我的应用程序中,在呈现 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)

【问题讨论】:

标签: swift user-interface uialertcontroller


【解决方案1】:

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

【讨论】:

  • 即使我也有同样的问题。你覆盖了哪个控制器的 UILabel 属性?
  • @AnirudhaMahale 我已经覆盖了UILable 类以添加一些导致此问题的自定义功能。曾经评论说它工作正常
  • @Kalikanth040494 我怎样才能找到它?
  • @Hilaj 我使用扩展添加了一些额外的自定义功能。
  • 我发现了我的问题,对于填充,我实际上使用的是一个扩展,这在我的情况下是这个问题
【解决方案2】:

过去几天我也面临同样的问题,但终于找到了解决方案。 我删除了“使用 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。 希望对你有帮助!!!

【讨论】:

    【解决方案3】:

    从 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。

    【讨论】:

    • 是的,我明白了。我显示的消息是动态的来自服务器。
    • 好的,但是我使用了您的代码并且问题没有出现,没有消息被截断。你必须做一些不同的事情......见我上面的编辑。
    • 谢谢我的问题。我正在覆盖 UILabel 属性,这就是发生这种奇怪行为的原因。我删除了代码,现在它工作正常
    【解决方案4】:

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

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

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

    【讨论】:

    • 但我显示的动态消息可能由一行或两行组成
    • 啊,好吧,你可以数一下字符数,然后在每 n 个字符后插入一个 \n。但这不会那么漂亮,我认为有更好的解决方案。
    • 是的,我已经这样做了,但是如果第 n 个字符不是空格,则它将分割单个字符。我不知道为什么 UIAlertController 的行为发生了变化。
    • 是的,我没想到,您还可以检查空格并在空格后插入 \n。您从哪里获得动态消息?
    • 在执行 URLSessionRequest 时来自服务器。你能把我怎么做的代码给我吗?提前致谢
    猜你喜欢
    • 2019-08-23
    • 2017-02-24
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2019-04-25
    相关资源
    最近更新 更多