我用UIAlertViewController
来展示吐司。我将对齐设置为居中,并且工作正常。如果我将语言从英语切换为阿拉伯语,反之亦然,则消息始终与自然左对齐。
func displayToast(vc: UIViewController, message: String, seconds: Double = 2.0, completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
let messageText = NSMutableAttributedString(
string: message,
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body),
NSAttributedString.Key.foregroundColor: UIColor.gray
]
)
alert.setValue(messageText, forKey: "attributedMessage")
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds, execute: {
alert.dismiss(animated: true, completion: nil)
})
alert.modalPresentationStyle = .popover
if let popoverPresentationController = alert.popoverPresentationController {
popoverPresentationController.sourceView = vc.view
popoverPresentationController.sourceRect = vc.view.bounds
popoverPresentationController.permittedArrowDirections = []
}
vc.present(alert, animated: true, completion: completion)
}
如何在RTL开关上设置警报消息对齐中心?
问题是这条线完全违法:
alert.setValue(messageText, forKey: "attributedMessage")
你正在运行时的背后工作。您的结果将无法预测,您的应用可能会从App Store中被拒绝。
如果你想控制UIAlertController没有给你的格式,不要使用UIAlertController。改为制作真正的视图控制器。