Ios UIAlertController文本对齐

Ios UIAlertController文本对齐,ios,ios8,uialertcontroller,Ios,Ios8,Uialertcontroller,有没有办法更改iOS 8上UIAlertController内显示的消息的对齐方式 我认为访问子视图并更改UILabel的子视图已不可能了。会公开文本字段,因此可能会用于配置它们: 文本字段 警报显示的文本字段数组。只读 声明 迅捷的 目标-C 讨论 用这个 属性访问警报显示的文本字段。正文 字段按您添加到警报中的顺序排列 控制器。此顺序也对应于它们的排列顺序 显示在警报中 注意:尽管它说属性是只读的,但它返回一个文本字段对象引用数组,可用于修改控件。我已成功使用以下方法来对齐UIAlertCo

有没有办法更改iOS 8上UIAlertController内显示的消息的对齐方式

我认为访问子视图并更改UILabel的子视图已不可能了。

会公开文本字段,因此可能会用于配置它们:

文本字段

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

声明

迅捷的

目标-C

讨论

用这个 属性访问警报显示的文本字段。正文 字段按您添加到警报中的顺序排列 控制器。此顺序也对应于它们的排列顺序 显示在警报中


注意:尽管它说属性是只读的,但它返回一个文本字段对象引用数组,可用于修改控件。

我已成功使用以下方法来对齐UIAlertController的文本并设置其样式:

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而不是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")
Swift 4更新 Swift 5更新
导航到子视图树,直到找到标题和消息的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
然后可以像这样对齐文本

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

以下方法将设置文本对齐方式,同时保留默认字体大小

static void makeAlertControllerLeftAligned( UIAlertController* avc )
{
    NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
    paraStyle.alignment = NSTextAlignmentLeft;
    NSMutableAttributedString *atrStr =
        [[[NSMutableAttributedString alloc] initWithString:avc.message attributes:@{
             NSParagraphStyleAttributeName:paraStyle,
             NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleFootnote],
         }]
         autorelease];
    [avc setValue:atrStr forKey:@"attributedMessage"];
}
Swift 4.2+多行字符串换行缩进

我接受了他的回答,并将其作为一个方便的扩展:

public extension UIAlertController {

    func setMessageAlignment(_ alignment : NSTextAlignment) {
        let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.alignment = alignment

        let messageText = NSMutableAttributedString(
            string: self.message ?? "",
            attributes: [
                NSAttributedString.Key.paragraphStyle: paragraphStyle,
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
                NSAttributedString.Key.foregroundColor: UIColor.gray
            ]
        )

        self.setValue(messageText, forKey: "attributedMessage")
    }
}

然后您可以简单地设置对齐值:myAlert.setMessageAlignment.left

这些不就是您可以添加到UIAlertController的UITextFields吗?我只通过message属性显示一个文本,该属性的样式为项目符号列表,因此将受益于左对齐。@DominikKaisers是的,你是对的;也许您可以使用文本字段而不使用message属性?是的,这可能是一个解决方法。如果没有人有更简单的方法,我会尝试一下。这些是文本输入字段,而不是消息。似乎这是私人API使用,这是否进入了应用商店?@powerj1984是的,它进入了应用商店。它有什么私密性?有任何objective-c版本吗?@SonicMaster抱歉,但我没有objective-c的经验。我想,因为这主要是赋值和调用函数,所以转换应该很简单。NSArray*viewArray=[[[[alertController视图]子视图]第一对象]子视图]第一对象]子视图]第一对象]子视图]第一对象]子视图]第一对象]子视图];这是我从未见过的最嵌套的对象。是的。苹果真的不想让人们把它搞砸。制作自定义弹出警报比修补嵌套得太深的东西要好得多。他们从未兑现承诺,在2013年的一次会议上,他们提到他们添加了UIView属性,以便我们可以在其中放入任何我们想要的东西。。。实际上,我想这是当前API的正确现代答案!它就像一个符咒!含糖的谢谢你,伙计!有人进入应用商店了吗?这是直截了当的,看起来不太粗糙,应该稳定一段时间请在代码周围添加解释。以便OP和未来的读者能够轻松理解您的答案。
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];
static void makeAlertControllerLeftAligned( UIAlertController* avc )
{
    NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
    paraStyle.alignment = NSTextAlignmentLeft;
    NSMutableAttributedString *atrStr =
        [[[NSMutableAttributedString alloc] initWithString:avc.message attributes:@{
             NSParagraphStyleAttributeName:paraStyle,
             NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleFootnote],
         }]
         autorelease];
    [avc setValue:atrStr forKey:@"attributedMessage"];
}
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle

paragraphStyle.headIndent = 14

let messageText = NSMutableAttributedString(
    string: "... multiline string need linebreak indentation ...",
    attributes: [
        NSAttributedString.Key.paragraphStyle: paragraphStyle,
        NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
        NSAttributedString.Key.foregroundColor: UIColor.gray
    ]
)

alert.setValue(messageText, forKey: "attributedMessage")
yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;
public extension UIAlertController {

    func setMessageAlignment(_ alignment : NSTextAlignment) {
        let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.alignment = alignment

        let messageText = NSMutableAttributedString(
            string: self.message ?? "",
            attributes: [
                NSAttributedString.Key.paragraphStyle: paragraphStyle,
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
                NSAttributedString.Key.foregroundColor: UIColor.gray
            ]
        )

        self.setValue(messageText, forKey: "attributedMessage")
    }
}