ios - 在 UIAlertController 消息上显示 HTML

标签 ios swift uialertcontroller

我想知道是否有办法在 UIAlertController 的消息中显示 HTML。

Android 有办法做到这一点,但我找不到在 iOS 上做到这一点的方法。我现在正在使用 Swift 3。

最佳答案

我不想在我的项目中添加更多的类,所以我一直在寻找一种更直接的方法来完成它。我找到了 this link很有帮助。

我的最终代码如下所示:

-(void)viewDidAppear:(BOOL) animated {
    NSString *htmlString = @"<html><body><h1>HTML, baby!</h1><p>Try it.<p>You'll totally<br />love it.<br /><b>Sample Bold text</b></body></html>";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Welcome"
                                                                     message:htmlString
                                                              preferredStyle:UIAlertControllerStyleAlert];
    NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                         NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
    documentAttributes:nil error:nil];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        // OK button tappped.
        [self dismissViewControllerAnimated:YES completion:^{ }];
    }];
    [alert setValue:attributedStr forKey: @"attributedMessage"];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:true completion:nil];
}

我包含了 viewDidAppear 声明,因为我的输入错误很长一段时间导致 UIAlertController 无法出现,因为整个函数从未被调用过。

关于ios - 在 UIAlertController 消息上显示 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858762/

相关文章:

iphone - 自定义 UITabBar 背景图像在 iOS 5 及更高版本中不起作用

c# - TabbedPage 无法在使用 MAUI 的 iOS 上工作,而是显示空白页面

ios - 导航和选项卡 View Controller 未正确加载,然后跳转到位

ios - 请求桌面站点 WKWebview 在 Swift 4 中不工作

ios - Objective-C - 我希望 UIAlertController 在单击按钮时不被删除

ios - Flexbox 和 safari

ios - FTP 目录中的特殊字符

ios - 如何在自定义 navigationController 代码中定位特定的 viewController?

ios - 只要函数持续就显示 UIAlertController

ios - UIAlertController 半透明故障/错误