UIAlertController文本alignment

有没有办法改变在iOS 8的UIAlertController内显示消息的alignment方式?

我相信访问子视图并更改为UILabel是不可能的了。

我已经成功地使用了以下内容来alignment和设置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") 

你可以用标题做一个类似的事情,如果你使用"attributedTitle"的标题"attributedTitle"而不是"attributedMessage"

Swift 3更新

上面的代码在Swift 3中仍然有效,但代码必须稍作修改:

 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") 

导航到子视图树,直到find标题和消息的UILabels

 NSArray *viewArray = [[[[[[[[[[[[alertController view] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews]; UILabel *alertTitle = viewArray[0] UILabel *alertMessage = viewArray[1]; alertMessage.textAlignment = NSTextAlignmentLeft; 

但是,你可能想为它做一个类别

 @interface UIAlertController (ShowMeTheLabels) @property (nonatomic, strong) UILabel *titleLabel, *messageLabel; @end @implementation UIAlertController (ShowMeTheLabels) @dynamic titleLabel; @dynamic messageLabel; - (NSArray *)viewArray:(UIView *)root { NSLog(@"%@", root.subviews); static NSArray *_subviews = nil; _subviews = nil; for (UIView *v in root.subviews) { if (_subviews) { break; } if ([v isKindOfClass:[UILabel class]]) { _subviews = root.subviews; return _subviews; } [self viewArray:v]; } return _subviews; } - (UILabel *)titleLabel { return [self viewArray:self.view][0]; } - (UILabel *)messageLabel { return [self viewArray:self.view][1]; } @end 

那么你可以像这样alignment文本

 yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft; 

使用下面的代码

 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]; 

该属性公开文本字段,所以可能可以用来configuration它们:

文本框

警报显示的文本字段数组。 (只读)

宣言

迅速

 var textFields: [AnyObject]? { get } 

Objective-C的

 @property(nonatomic, readonly) NSArray *textFields 

讨论

使用此属性可以访问警报显示的文本字段。 文本字段按照您将其添加到警报控制器的顺序。 该顺序也对应于它们在警报中显示的顺序。

注意:尽pipe它表示该属性是readonly ,但它返回的是可用于修改控件的文本字段对象引用数组。