ios - 如何将 UIAlertController 操作表标题垂直居中对齐?

标签 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”)

最佳答案

试试这个代码,它对我来说工作得很好。

        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)
}

关于ios - 如何将 UIAlertController 操作表标题垂直居中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53753298/

相关文章:

ios - 如何为 UIAlertController(ActionSheet) 创建全局函数以在许多项目类中访问

ios - IOS 中启动屏幕和初始屏幕之间的故障

ios - 将文本绘制到 UIView,但 CATextLayer 不起作用

ios - Swift 和 Objective-C 无法分配全局变量

x代码 7 : how to fix initialization of variable was never used consider replacing with assignment to '_' or removing it

swift - 反转字符串值的顺序

ios - 扩展 UIAlertController 便利初始化警告

ios - UIAlertController(ActionSheet)在设置tintColor后改变形状

ios - iPhone 上的流畅 UI 布局

ios - UITextView 字体为零