带有NSAttributedString(s)的简单UIAlertView

6

我正在寻找一种简单的方法,以类似下面这样的非常简单的消息框为例,使用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不是选项)


既然 UIAlertViewUIAlertController 都不支持属性字符串,那么你要么找到一个第三方替代品,要么自己动手写一个。 - rmaddy
有没有什么第三方库的建议?我在 cocoa controls 上找了一下,但它们似乎都比我需要的要复杂得多。 - theshadow124
猜测最好的方法是创建自己的,这里有一个很好的例子:https://dev59.com/1XfZa4cB1Zd3GeqPR3no#19119307 - theshadow124
4个回答

15
将您的属性字符串添加到标签中,并将其作为辅助视图添加到警报中。
```

将您的属性字符串添加到标签中,并将其作为辅助视图添加到警报中。

```
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


这实现了我想要做的事情,可能需要担心长消息的大小。谢谢! - theshadow124
1
@AshishKakkad,感谢您的回答-我已经找到了改进其用户界面的方法。+1给你,并为我的SO做出一些贡献。[附言:添加了我的答案] - Hemang
@NiibFouda 欢迎。但请使用 UIAlertController - Ashish Kakkad
@AshishKakkad,我无法在UIAlertController中使用“accessoryView”,我需要在警报中添加可点击的超链接。 - Niib Fouda

5

我从@AshishKakkad的回答中得到了灵感(+1)。不过,它的用户界面不是很清晰。所以我向您展示一种使用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;
}

enter image description here


2

不要忘记设置:

lbl.numberOfLines = 0;

当你有多行时


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接