UIActionSheet/UIAlertController multiline text
Asked Answered
B

4

7

This is the code I am writing to display UIActionSheet.

actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"updateresponseforrecurring", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:nil otherButtonTitles:
                   NSLocalizedString(@"updateresponseonlyforthis", nil),
                   NSLocalizedString(@"updateresponseforallactivities", nil),
                   nil];
    actionSheet.tag = 2;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];  

This is what I get using this :

enter image description here

Clearly, second option is longer and thus the size gets smaller to accommodate the width.
But I want the same font size for all the options which leaves me with multiline. Also tried with UIAlertController but not able to set multiline text. How to achieve this ?

Barbicel answered 19/10, 2015 at 13:25 Comment(0)
S
27

This seems to work in iOS 10 and Swift 3.1:

UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 2
Serrell answered 17/3, 2017 at 19:9 Comment(2)
You can also set numberOfLines to 0. This allows as many lines as necessary.Gambrell
This sets numberOfLines = 2 for all UIAlertControllers... What if I just want to use this for only one specific UIAlertController?Bioecology
S
2

try this

let alert = UIAlertController(title: title,
                              message: "you message go here",
                              preferredStyle: 
UIAlertControllerStyle.alert)

let cancelAction = UIAlertAction(title: "Cancel",
                                 style: .cancel, handler: nil)

alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)

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

UILabel.appearance(whenContainedInInstancesOf: 
[UIAlertController.self]).lineBreakMode = .byWordWrapping
Savagism answered 26/3, 2018 at 14:0 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Bowfin
M
1

This is not possible with the standard UIAlertController or UIAlertView. I would recommend you to make it shorter. Why don't you make an alert and type something like this:

Do you want to update the response only for this instance or for all activities in this series.

The answers would be these:

  • Only this instance
  • All activities
Mythopoeia answered 19/10, 2015 at 13:55 Comment(1)
If my answer did help you to solve your problem, please select it as accepted answer. Thanks!Mythopoeia
M
1

In iOS 10:

If you want to apply it to all UIAlertController, you can use these lines of code:

[[UILabel appearanceWhenContainedIn:[UIAlertController class], nil] setNumberOfLines:2];
[[UILabel appearanceWhenContainedIn:[UIAlertController class], nil] setFont:[UIFont systemFontOfSize:9.0]];

put this in didFinishLaunchingWithOptions method of the AppDelegate.

Motherland answered 27/1, 2017 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.