Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController text alignment

Is there a way to change the alignment of the message displayed inside a UIAlertController on iOS 8?

I believe accessing the subviews and changing it for the UILabel is not possible anymore.

like image 954
dkaisers Avatar asked Sep 21 '14 18:09

dkaisers


People also ask

Which text alignment is best?

The left-aligned text results in much better content readability, so all books, articles & newspapers are written this way. The left-aligned text helps to avoid unnecessary eye jumps, making the whole copy much easier to follow.

How do I change the alignment of the text on the left side?

Align the text left or right Select the text that you want to align. On the Home tab, in the Paragraph group, click Align Left or Align Right .


2 Answers

I have successfully used the following, for both aligning and styling the text of UIAlertControllers:

let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.Left  let messageText = NSMutableAttributedString(     string: "The message you want to display",     attributes: [         NSParagraphStyleAttributeName: paragraphStyle,         NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody),         NSForegroundColorAttributeName : UIColor.blackColor()     ] )  myAlert.setValue(messageText, forKey: "attributedMessage") 

You can do a similar thing with the title, if you use "attributedTitle", instead of "attributedMessage"

Swift 3 update

The above still works in Swift 3, but the code has to be slightly altered to this:

let messageText = NSMutableAttributedString(     string: "The message you want to display",     attributes: [         NSParagraphStyleAttributeName: paragraphStyle,         NSFontAttributeName : UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),         NSForegroundColorAttributeName : UIColor.black     ] )  myAlert.setValue(messageText, forKey: "attributedMessage") 

Swift 4 update

let messageText = NSMutableAttributedString(     string: "The message you want to display",     attributes: [         NSAttributedStringKey.paragraphStyle: paragraphStyle,         NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),         NSAttributedStringKey.foregroundColor: UIColor.black     ] )  myAlert.setValue(messageText, forKey: "attributedMessage") 

Swift 5 update

let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = alignment let messageText = NSAttributedString(     string: "message",     attributes: [         NSAttributedString.Key.paragraphStyle: paragraphStyle,         NSAttributedString.Key.foregroundColor : UIColor.primaryText,         NSAttributedString.Key.font : UIFont(name: "name", size: size)     ] )  myAlert.setValue(messageText, forKey: "attributedMessage") 
like image 70
Eduardo Avatar answered Sep 17 '22 18:09

Eduardo


Use the below code

UIAlertController * alert = [UIAlertController                                  alertControllerWithTitle:[title lowercaseString]                                  message:[message lowercaseString]                                  preferredStyle:UIAlertControllerStyleAlert];      if (alertStyle == kUIAlertStylePlainText)     {         [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {          }];     }      if (okTitle) {         UIAlertAction* ok = [UIAlertAction actionWithTitle:[okTitle lowercaseString] style:UIAlertActionStyleDefault                                                    handler:^(UIAlertAction * action) {                                                        callback(alert, action, nil);                                                    }];         [alert addAction:ok];     }      if (cancelTitle) {         UIAlertAction* cancel = [UIAlertAction actionWithTitle:[cancelTitle lowercaseString] style:UIAlertActionStyleDefault                                                        handler:^(UIAlertAction * action) {                                                            callback(alert, nil, action);                                                        }];         [alert addAction:cancel];     }       NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];     paraStyle.alignment = NSTextAlignmentLeft;      NSMutableAttributedString *atrStr = [[NSMutableAttributedString alloc] initWithString:[message lowercaseString] attributes:@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont systemFontOfSize:13.0]}];      [alert setValue:atrStr forKey:@"attributedMessage"];     [viewInstance presentViewController:alert animated:YES completion:nil]; 
like image 36
Ganesh Bavaskar Avatar answered Sep 16 '22 18:09

Ganesh Bavaskar