UIAlertController 文本对齐

问题描述 投票:0回答:9

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

我相信访问子视图并更改 UILabel 已不再可能。

ios ios8 uialertcontroller
9个回答
77
投票

我已成功使用以下内容来对齐和设置 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"
而不是
"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 更新

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 更新

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

10
投票

使用以下代码

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

10
投票

我采纳了@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")
    }
}

然后您可以简单地设置对齐值:

myAlert.setMessageAlignment(.left)


6
投票

导航到子视图树,直到到达标题和消息的 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;

3
投票

Swift 4.2 + 多行字符串换行缩进

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

2
投票

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

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

0
投票

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

文本字段

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

声明

环球银行金融电信协会

var textFields: [AnyObject]? { get }

目标-C

@property(nonatomic, readonly) NSArray *textFields

讨论

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

注意:尽管它说属性是

readonly
,但它返回一个文本字段对象引用数组,可用于修改控件。


0
投票

我注意到有些答案使用了私有 API,这是不安全的。我有一个更安全、更简单的方法来实现同样的目标。这是代码:

extension UIAlertController {

    func demo() {
        title = "My Title"
        message = "My subtitle\nsecond line\nthird line"
        customMessageLabel { label in
            label.textAlignment = .left
        }
    }

    func customMessageLabel(config: (UILabel) -> Void) {
        let originMessage = self.message
        let secret = UUID().uuidString
        self.message = secret
        if let messageLabel = findMessageLabel(in: view, secret: secret) {
            config(messageLabel)
        }
        self.message = originMessage
    }

    @discardableResult
    private func findMessageLabel(in targetView: UIView, secret: String) -> UILabel? {
        if let label = targetView as? UILabel, label.text == secret {
            return label
        } else {
            for subview in targetView.subviews {
                if let label = findMessageLabel(in: subview, secret: secret) {
                    return label
                }
            }
            return nil
        }
    }

}

-1
投票
yourAlertController.messageLabel.textAlignment = NSTextAlignmentLeft;
© www.soinside.com 2019 - 2024. All rights reserved.