Add multiple line text to UIAlertAction in correct size

不羁岁月 提交于 2020-05-14 10:55:34

问题


I'm showing the 2 text values in UIAlertAction.

I used the following code.

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)

But the view size is not increased automatically and there is no enough space at the top and bottom.

How can I figure out this?

来源:https://stackoverflow.com/questions/61216417/add-multiple-line-text-to-uialertaction-in-correct-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!