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

时间:2018-07-12 07:09:28

标签: ios swift uialertcontroller

我正在使用警报控制器(标题和消息为QFuture)来显示警报,但由于不是多行,消息被截断了。
因此,无论如何要将消息多行显示,或者我可能需要为此更改任何属性,请提出建议。 该消息来自服务器,这就是为什么无法添加QtConcurrent::run的原因。

请发布任何解决方案。 预先感谢!

我只是使普通演示正常运行,但是我不知道为什么它不能在我的项目代码中正常工作?

这是我的代码:

NSMutableAttributedString

3 个答案:

答案 0 :(得分:2)

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

示例:

let longString = ""

当您编写跨多个字符串 行确保您的内容开始于 自己排成一行,并以三个结尾 引号也可以自己一行。 多行字符串还可以让您写“引号” 自由地在琴弦内,这很棒! “”“

因此,您的代码应为:

let longTextMessage = ""

当您编写跨多个字符串 行确保您的内容开始于 自己排成一行,并以三个结尾 引号也可以自己一行。 多行字符串还可以让您写“引号” 自由地在琴弦内,这很棒! “”“

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)

答案 1 :(得分:0)

let strAttributes = [NSFontAttributeName:UIFont.boldSystemFont(ofSize:18.0)]        let alrtTitleStr = NSMutableAttributedString(string:“将来自服务器的标题消息”,属性: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)

答案 2 :(得分:0)

已解决 如果您在项目中编写这种类型的代码,那么它将受到警报控制器的某些默认属性的影响。要删除这种类型的扩展警报控制器,请在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
}