UIAlertController не отображает текст в центре при переключении RTL в 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 выровнялось сообщение с предупреждением?


person johndoe    schedule 24.02.2019    source источник


Ответы (1)


Проблема в том, что эта строка абсолютно недопустима:

alert.setValue(messageText, forKey: "attributedMessage")

Вы работаете за спиной среды выполнения. Ваши результаты будут непредсказуемыми, и ваше приложение может быть отклонено из App Store.

Если вы хотите контролировать форматирование, недоступное для UIAlertController, не используйте UIAlertController. Вместо этого сделайте настоящий контроллер представления.

person matt    schedule 24.02.2019
comment
Я взял эту часть из ответа SO по адресу: stackoverflow.com/a/26949674/10900045 - person johndoe; 24.02.2019