UIAlertController文本对齐

dka*_*ers 39 ios ios8 uialertcontroller

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

我相信不再可能访问子视图并为UILabel更改它.

Edu*_*rdo 60

我成功地使用了以下内容,用于对齐和设计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")
Run Code Online (Sandbox Code Playgroud)

如果你使用的话"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")
Run Code Online (Sandbox Code Playgroud)

Swift 4更新

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")
Run Code Online (Sandbox Code Playgroud)

  • 看起来这是私有API使用,这是否进入App Store? (2认同)

小智 8

使用下面的代码

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];
Run Code Online (Sandbox Code Playgroud)


Der*_*328 7

导航到子视图树,直到您到达标题和消息的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;
Run Code Online (Sandbox Code Playgroud)

但是,您可能希望为其创建一个类别

@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
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样对齐文本

yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;
Run Code Online (Sandbox Code Playgroud)

  • `NSArray*viewArray = [[[[[[[[[[[[[alertController view] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews] firstObject] subviews];`这大部分都是蛋糕嵌套的ObjC我从未见过o.0 (17认同)
  • 是.Apple并不真的希望别人搞砸它.制作自定义弹出警报比修补如此深层嵌套的东西要好得多. (6认同)
  • 他们从来没有履行过他们的承诺,在2013年的一次会议中,他们提到他们添加了一个UIView属性,以便我们可以把任何我们想要的东西放在那里......它从未进入过. (2认同)

Ari*_*ner 6

我接受了@Amos的回答并将其变成了一个方便的扩展:

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

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


tro*_*foe 0

此属性公开文本字段,因此可以用于配置它们:

文本字段

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

宣言

迅速

var textFields: [AnyObject]? { get }
Run Code Online (Sandbox Code Playgroud)

目标C

@property(nonatomic, readonly) NSArray *textFields
Run Code Online (Sandbox Code Playgroud)

讨论

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

注意:即使它说属性是readonly,它也会返回一个文本字段对象引用数组,可用于修改控件。

  • 这些是文本输入字段,而不是消息。 (4认同)
  • @DominikKaisers 是的,你是对的;也许您可以使用文本字段而不使用消息属性? (2认同)