Добавьте многострочный текст в UIAlertAction в правильном размере - PullRequest
0 голосов
/ 14 апреля 2020

Я показываю 2 текстовых значения в UIAlertAction.

Я использовал следующий код.

UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0

let actionSheetController = UIAlertController(title: nil, message: "Call", preferredStyle: .actionSheet)
actionSheetController.view.tintColor = UIColor.red

for key in phoneNumbers.keys {
    let phoneNumber = phoneNumbers[key] ?? ""
    let actionTitle = "\(key)\n\(phoneNumber)"
    let phoneNumberAction = UIAlertAction(title: actionTitle, style: .default) { action -> Void in
        self.makeCall(phoneNumber)
    }
    actionSheetController.addAction(phoneNumberAction)

    let attributedText = NSMutableAttributedString(string: actionTitle)

    let range = NSRange(location: key.count, length: attributedText.length - key.count)
            attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.gray, range: range)

    guard let label = (phoneNumberAction.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return }
    label.attributedText = attributedText
}

let cancelAction = UIAlertAction(title: LOCALSTR("Cancel"), style: .cancel) { action -> Void in

}
actionSheetController.addAction(cancelAction)

present(actionSheetController, animated: true, completion: nil)

Но размер вида не увеличивается автоматически, и в нем недостаточно места верх и низ.

enter image description here

Как мне это выяснить?

...