当 RTL 切换 iOS 时,UIAlertController 不在中心显示文本
UIAlertController not displaying text in center when RTL switch in iOS
我用 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。而是制作一个真正的视图控制器。
我用 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。而是制作一个真正的视图控制器。