警报控制器描述消息不是多行的,它的尾巴被截断了。?

Alert controller description message are not multiline , it get truncated tail.?

我正在使用警报控制器(NSMutableAttributedString 用于标题和消息)来显示警报,但我的消息被截断了,因为它不是多行的。
所以无论如何要多行消息或者我可能需要更改任何 属性 请建议。 该消息来自服务器,这就是无法添加 \n.

的原因

请post任何解决方案。 提前致谢!

我只是做了一个正常的演示,它运行得很好,但我不知道为什么它在我的项目代码中不起作用?

这是我的代码:

    self.navigationController?.navigationBar.isHidden = true
    let alrtTitleStr = NSMutableAttributedString(string:"Title message which will from server")
    alrtTitleStr.addAttribute(NSAttributedStringKey.font, value: UIFont.boldSystemFont(ofSize: 18.0) , range: NSRange(location: 0, length: alrtTitleStr.length))

    let alrtMessage = NSMutableAttributedString(string:"Here is the message which comes from server and it should be display in muultiline.")
    alrtMessage.addAttribute(NSAttributedStringKey.font, value:  UIFont.systemFont(ofSize:16.0) , range: NSRange(location: 0, length: alrtMessage.length))

    let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
    alertController.setValue(alrtTitleStr, forKey: "attributedTitle")
    alertController.setValue(alrtMessage, forKey: "attributedMessage")

    let btnYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
       //next process
    })
    let btnNo = UIAlertAction(title: "No", style: .default, handler: { action in
        //next process
    })

    alertController.addAction(btnYes)
    alertController.addAction(btnNo)
    self.present(alertController, animated: true, completion: nil)

因为 Swift 4 您可以使用多行字符串:

示例:

let longString = ""

当你写一个跨越多个的字符串 lines 确保你开始它的内容 自己排成一行,并以三个结尾 引号也在他们自己的一行上。 多行字符串也让你写 "quote marks" 自由地在你的弦内,这太棒了! """

因此,您的代码将是:

let longTextMessage = ""

当你写一个跨越多个的字符串 lines 确保你开始它的内容 自己排成一行,并以三个结尾 引号也在他们自己的一行上。 多行字符串也让你写 "quote marks" 自由地在你的弦内,这太棒了! """

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)

让 strAttributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 18.0)] 让 alrtTitleStr = NSMutableAttributedString(字符串:"Title message which will from server",属性:strAttributes)

    let strAttributesMs = [NSFontAttributeName:UIFont.systemFont(ofSize:16.0)]
    let alrtMessage = NSMutableAttributedString(string: "Here is the message which comes from server and it should be display in muultiline. Here is the message which comes from server and it should be display in muultiline. Here is the message which comes from server and it should be display in muultiline. Here is the message which comes from server and it should be display in muultiline.", attributes: strAttributesMs)

    let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
    alertController.setValue(alrtTitleStr, forKey: "attributedTitle")
    alertController.setValue(alrtMessage, forKey: "attributedMessage")

    let btnYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
        //next process
    })
    let btnNo = UIAlertAction(title: "No", style: .default, handler: { action in
        //next process
    })

    alertController.addAction(btnYes)
    alertController.addAction(btnNo)
    self.present(alertController, animated: true, completion: nil)

已解决 如果您在项目中编写此类代码,则会影响警报控制器的某些默认属性。要删除这种类型的扩展警报控制器,请在 swift 4

中使用默认的多行属性
override open var intrinsicContentSize: CGSize {
    guard let text = self.text else { return super.intrinsicContentSize }

    var contentSize = super.intrinsicContentSize
    var textWidth: CGFloat = frame.size.width
    var insetsHeight: CGFloat = 0.0

    if let insets = padding {
        textWidth -= insets.left + insets.right
        insetsHeight += insets.top + insets.bottom
    }

    let newSize = text.boundingRect(with: CGSize(width: textWidth, height: CGFloat.greatestFiniteMagnitude),
                                    options: NSStringDrawingOptions.usesLineFragmentOrigin,
                                    attributes: [NSAttributedStringKey.font: self.font], context: nil)

    contentSize.height = ceil(newSize.size.height) + insetsHeight

    return contentSize
}