gpt4 book ai didi

ios - 带有 NSAttributedString(s) 的简单 UIAlertView

转载 作者:可可西里 更新时间:2023-11-01 04:07:48 26 4
gpt4 key购买 nike

我正在寻找一种简单的方法来使用 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

关于ios - 带有 NSAttributedString(s) 的简单 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30136122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com