ios - 当 iOS 中的 RTL 切换时,UIAlertController 不在中心显示文本

标签 ios swift right-to-left arabic-support

我使用 UIAlertViewController 来显示 toast 。我将对齐方式设置为居中,效果很好。如果我将语言从英语切换为阿拉伯语,反之亦然,消息总是自然左对齐。

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。而是制作一个真正的 View Controller 。

关于ios - 当 iOS 中的 RTL 切换时,UIAlertController 不在中心显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54851745/

相关文章:

ios - 自定义键盘 ios swift 中的 Qwerty Azerty

ios - FCM 通知不会出现在 iOS 12 中

c# - 将 DataGridView 的特定单元格设置为 "Left to Right"的对齐方式

html - 非字母数字的输入方向 rtl 问题

ios - 应用程序在 tableview 的 reloadData 方法上崩溃

javascript - React native 本地化 : Global variable

ios - UIImagePickerController - 在相机 View 中选择保存的图像

ios - 如何将 gif 添加到 iPhone 邮件中的附件?

swift - 如何阻止 swiftui 在 HStack 中将内容移出屏幕?

android - 如何在 Android 的 strings.xml 中为 rtl 字符串添加换行符?