All the alert dialog message and textField have been changed to single line. Please checkout the image
Asked Answered
R

5

9

Previously all the dialog and textField are working well. But not I do not know how these TextFields are suddenly changed to single line with triple. (Like some Message 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

Reconstructive answered 14/9, 2017 at 6:39 Comment(10)
where is the imageChurchman
Please ckeckout the images mentioned in the post. @AnbuReconstructive
i tried your code its showing perfectly, I think some where you modifed appearanceChurchman
It's working perfectly till yesterday but today I do not what happens. Did you ever face this type of issue ?Reconstructive
ok how do you present the feedback view and how do you design thisChurchman
It is just a overlapping subview.Reconstructive
no no some where problem not in yur alertcontrollerChurchman
No no, it not just about alertDialog message all the TextFields that are multiline are also changed to single line with three dots.Reconstructive
is this possible to attach your projectChurchman
Sorry no. I think this is due to some xcode issue/bug because its working well previously. I think it just not about only alertDialog message all the TextFields that are multiline are also changed to single line with three dots.Reconstructive
R
0

Solved Finally

I solved this problem by making the custom UILable inside the UIViewController. This may not be the good practice, so kindly let me know if someone find the better solution then this.

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)

}
Reconstructive answered 22/12, 2017 at 4:34 Comment(3)
Could you ever able to find the root cause for this issue instead of the above workaround? I am facing the same issue that all of my alert messages changed to single line and I am sure that they were working well before.Judicious
Sorry, not till today.Reconstructive
@BatuhanC., the instruction on this StackOverflow page helped me; https://mcmap.net/q/1316169/-uialertview-automatic-newline-gone-in-ios8Gemination
J
5

I had the same problem and solved it after 3 days and nights. Since the UIAlertViewController uses UILabel to show the message, I was firmly searching all over the project for something modifying the UILabel. I realized that no search result includes anything from some pods that definitely has "label" keywords in their function names and such. I decided to download the source codes for all of the pods from their repositories and searched recursively inside them with another simple text editor and voila! Some guy decided to override the default UILabel class instead of subclassing it in their pod. The culprit lines were

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

These did not show up in the search results by using the search function in XCode as I searched for UILabel extensions to begin with. So, I recommend you to open any 3rd party framework's source codes in your project and search inside them separately. There is most definitely something messing with the UILabel class.

Judicious answered 28/2, 2018 at 10:9 Comment(1)
You save my life! I was having this problem few months ago.Unpleasantness
T
4

Add line break characters (\n) to your message.

Theona answered 12/12, 2017 at 10:27 Comment(1)
Sorry !! This one is not good solution for me. I want to get data from web and have to show in alert dialog. Already implemented using \n new line tag.Reconstructive
B
1

You should use attributedMessage String with setValue method of UIAlertController here just 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)
Baur answered 12/12, 2017 at 11:30 Comment(2)
Please recheck your answer before answering to the question. Issue: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'Reconstructive
Not working, Please do not give unrealistic answer. @BaurReconstructive
G
1

You have to set numberOfLines property of UILabel, because default value is 1 (single line) & and value of 0 means no limit, if the height of the text reaches the numberOfLines of lines or the height of the view is less than the numberOfLines of lines allowed, the text will be truncated using the line break mode.

if #available(iOS 9.0, *) {
        UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0
    } else {
        // Fallback on earlier versions
    }
Glassblowing answered 19/12, 2017 at 9:4 Comment(1)
Sorry, don't work for me. Please take a look at first image, I use this code to show the dialog. let alert = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) viewController.present(alert, animated: true, completion: nil)Reconstructive
R
0

Solved Finally

I solved this problem by making the custom UILable inside the UIViewController. This may not be the good practice, so kindly let me know if someone find the better solution then this.

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)

}
Reconstructive answered 22/12, 2017 at 4:34 Comment(3)
Could you ever able to find the root cause for this issue instead of the above workaround? I am facing the same issue that all of my alert messages changed to single line and I am sure that they were working well before.Judicious
Sorry, not till today.Reconstructive
@BatuhanC., the instruction on this StackOverflow page helped me; https://mcmap.net/q/1316169/-uialertview-automatic-newline-gone-in-ios8Gemination

© 2022 - 2024 — McMap. All rights reserved.