如何在中间垂直对齐UIAlertController操作表标题?

时间:2018-12-13 00:16:59

标签: ios swift uialertcontroller

我找不到如何将UIAlertcontroller操作表的标题垂直对齐。以下是我的代码,有帮助吗?

更新。

利用下面提供的提示,我做了一些挖掘,发现如果列表很长,标题/消息会被推高,否则如果列表很短,标题/消息会居中。查看其他屏幕截图

Action Sheet

Alert

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.left
    // paragraphStyle.minimumLineHeight = // Is this the one? How to get the value required?

    let attributedString = NSAttributedString(string: "Select Sorting Option", attributes: [
        NSAttributedStringKey.paragraphStyle: paragraphStyle,
        NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20),
        NSAttributedStringKey.foregroundColor : UIColor.darkGray
        ])
    let alert = UIAlertController(title: "", message: "",  preferredStyle: .actionSheet)
    alert.setValue(attributedString, forKey: "attributedTitle”)

1 个答案:

答案 0 :(得分:0)

尝试此代码对我来说很好。

        let alertView = UIAlertController(title: "Demo Alert", message: "", preferredStyle: .alert)
    alertView.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

    let paragraphStyle = NSMutableParagraphStyle()

    paragraphStyle.alignment = .center

    let messageText = NSMutableAttributedString(
        string: "Select Sorting Option",
        attributes: [
            NSAttributedString.Key.paragraphStyle: paragraphStyle,
            NSAttributedString.Key.font : UIFont.preferredFont(forTextStyle: .body),
            NSAttributedString.Key.foregroundColor : UIColor.black
        ]
    )

    alertView.setValue(messageText, forKey: "attributedMessage")
    present(alertView, animated: true, completion: nil)
}