带有 NSAttributedString(s) 的简单 UIAlertView

Simple UIAlertView with NSAttributedString(s)

我正在寻找一种简单的方法来使用 NSAttributedString 和一个非常简单的消息框,类似于:

NSString *new_item = [NSString stringWithFormat:@"<span style=\"font-family: Helvetica Neue; font-size: 12.0\">%@</span>", @"MOTD HTML String downloaded from website"];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[new_item dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MOTD"
                                                   message:attrStr
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
[alert show];

我上面的代码采用从服务器下载的 HTML 格式的字符串,确保文本大小适合屏幕,然后尝试将 NSAttributedString 发送到 UIAlertView。但是 UIAlertView 不喜欢这样。解决这个问题的最简单方法是什么?(非 HTML 格式的 MOTD 不是一个选项)

将您的属性字符串添加到标签并将其作为 assessoryView 添加到警报

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Test Attributed String" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:@"Hello red"];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
lbl.attributedText = attributedStr;
lbl.textAlignment = NSTextAlignmentCenter;
[alert setValue:lbl forKey:@"accessoryView"];
[alert show];

现在 UIAlertView 天已弃用。您可以使用 UIAlertController.

别忘了设置:

lbl.numberOfLines = 0;

当你有多行时

我从@AshishKakkad 的回答中得到了一个想法 (+1)。但是,它的 UI 不正确可见。因此,我将向您展示一种使用 attributedString.

格式化消息的方法

我是这样做的:

NSMutableString *message = [NSMutableString string];
NSString *title = @"Message Heading";
[message appendString:title];
[message appendString:@"\n\n• Topic 1"];
[message appendString:@"\n• Topic 2"];
[message appendString:@"\n• Topic 3\n"]; //Important, I have added '\n' at last to have some extra space at bottom.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"" delegate:self cancelButtonTitle:@"CancelButton" otherButtonTitles:@"OtherButton", nil];
//need to set a label as `accessoryView` of an alert.
[alert setValue:[self getLabelWithMessage:message withTitle:title] forKey:@"accessoryView"];
[alert show];

- (UILabel *)getLabelWithMessage:(NSMutableString *)message withTitle:(NSString *)title {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSTextAlignmentLeft];
    paragraphStyle.paragraphSpacing = 2.0f;
    paragraphStyle.headIndent = 20.0;
    paragraphStyle.firstLineHeadIndent = 20.0;
    paragraphStyle.tailIndent = -20.0;

    NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:message];
    [attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [message length])];
    [attribString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, [message length])];
    [attribString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12.f] range:NSMakeRange(0, [title length])];

    UILabel *label = [[UILabel alloc] init];
    [label setAttributedText:attribString];
    [label setNumberOfLines:0];
    [label sizeToFit];
    return label;
}

作为私有 API 的替代方法,您可以使用 https://github.com/AntonPoltoratskyi/NativeUI

pod 'NativeUI/Alert', '~> 1.0'

它看起来很像本机警报,但足够可配置。

请查看了解更多详情。