The entire warning dialog box and text box have been changed to one line. Please check the image.

Previously, all dialog and text fields worked well. But I do not know how these TextFields suddenly change on one line with a triple. (Like some post here ...)

    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)

Demo 1 Demo 3

+6
source share
5 answers

Add line characters (\ n) to the message.

+2
source

You should use attributedMessageString with the setValuemethod UIAlertControllerhere as follows:

    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)
+1
source

, UILable UIViewController. , , - , .

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)

}
+1

numberOfLines UILabel, 1 ( ), 0 , OfLines , , .

if #available(iOS 9.0, *) {
        UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0
    } else {
        // Fallback on earlier versions
    }
0

, 3 . UIAlertViewController UILabel , -, UILabel. , , "" .. ! UILabel , .

extension UILabel { ... override open func draw(_ rect: CGRect) { ... } override open var intrinsicContentSize: CGSize { ... } ... }

They did not appear in search results using the search function in Xcode when I was looking for UILabel extensions to get started. Therefore, I recommend that you open any third-party source code for the frameworks in your project and search inside them separately. There is definitely something that is messing with the UILabel class.

0
source

All Articles