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.
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.
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 .
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"
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")
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")
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")
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With