带有NSAttributedString的简单UIAlertVi

带有NSAttributedString的简单UIAlertVi

本文介绍了带有NSAttributedString的简单UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a simple way to use NSAttributedString with a very simple message box similar to:

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不是一个选项)

My above code takes a HTML formatted string that has been downloaded from a server, makes sure the text size will fit the screen properly, then tries to send the NSAttributedString to the UIAlertView. But UIAlertView does not like this. what would be the simplest way around this problem?(Non HTML formatted MOTD is not an option)

推荐答案

将你的属性字符串添加到标签并将其添加为assessmentoryView以提醒

Add your attributed string to label and add it as assessoryView to alert

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

Now a days UIAlertView is deprecated. You can use UIAlertController.

这篇关于带有NSAttributedString的简单UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:41