所有警报对话框消息和textField已被更改为单行。 请检查图像
以前所有的对话框和textField都运行良好。 但不是我不知道这些TextFields是如何突然变成单线三合一的。 (像这里的一些消息…)
let alert = UIAlertController(title: "Cancel Booking !!", message: "Are you sure you want to cancel your booking?", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "No", style: .default, handler: nil)) alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: self.cancelMessageDialog)) self.present(alert, animated: true, completion: nil)
将换行符(\ n)添加到您的消息。
您应该在UIAlertController
setValue
方法中使用attributedMessage
string,如下所示:
let attributedString = NSAttributedString(string: "My long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long textMy long text", attributes: [NSAttributedStringKey.font : UIFont(name: "Avenir-Light", size: 20)!]) let alert = UIAlertController(title: "Title", message: "", preferredStyle: .alert) alert.setValue(attributedString, forKey: "attributedMessage") let yesAction = UIAlertAction(title: "Yes", style: .default) { (action) in } let noAction = UIAlertAction(title: "No", style: .cancel) { (action) in } alert.addAction(noAction) alert.addAction(yesAction) present(alert, animated: true, completion: nil)
您必须设置UILabel
numberOfLines
属性,因为默认值为1(单行),而value的值为0意味着没有限制,如果文本的高度达到行的numberOfLines或视图的高度小于numberOfLines行允许,文本将被截断使用换行符模式。
if #available(iOS 9.0, *) { UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0 } else { // Fallback on earlier versions }
最后解决
我通过在UIViewController中创build自定义的UILable来解决这个问题。 这可能不是好的做法,所以请让我知道,如果有人find更好的解决scheme,那么这个。
func showTestAlert(message:String , viewController:UIViewController){ let customUiLableView:UILabel let alert:UIAlertController if((message.count) < 100){ alert = UIAlertController(title: "", message: "\n\n\n\n", preferredStyle: .alert) customUiLableView = UILabel(frame: CGRect(x: 10, y: 0, width: 250, height: 120)) customUiLableView.numberOfLines = 4 }else if((message.count) < 200){ alert = UIAlertController(title: "", message: "\n\n\n\n\n\n", preferredStyle: .alert) customUiLableView = UILabel(frame: CGRect(x: 10, y: 0, width: 250, height: 160)) customUiLableView.numberOfLines = 6 }else{ alert = UIAlertController(title: "", message: "\n\n\n\n\n\n\n\n", preferredStyle: .alert) customUiLableView = UILabel(frame: CGRect(x: 10, y: 0, width: 250, height: 200)) customUiLableView.numberOfLines = 8 } customUiLableView.text = message customUiLableView.textAlignment = .center customUiLableView.textColor = UIColor.darkGray customUiLableView.font = UIFont(name: "Helvetica", size: 16.0) let action = UIAlertAction(title: "OK", style: .default, handler: nil) alert.view.addSubview(customUiLableView) alert.addAction(action) viewController.present(alert, animated: true, completion: nil) }