菜鸟教程小白 发表于 2022-12-11 18:43:22

ios - 在 UIAlertController 消息上显示 HTML


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

<p>Android 有办法做到这一点,但我找不到在 iOS 上做同样事情的方法。我现在正在使用 Swift 3。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我不想在我的项目中添加更多的类,所以我一直在寻找一种更直接的方法来做到这一点。我找到了<a href="https://stackoverflow.com/questions/4217820/convert-html-to-nsattributedstring-in-ios" rel="noreferrer noopener nofollow">this link</a>很有帮助。</p>

<p>我的最终代码如下所示:</p>

<pre><code>-(void)viewDidAppear:(BOOL) animated {
    NSString *htmlString = @&#34;&lt;html&gt;&lt;body&gt;&lt;h1&gt;HTML, baby!&lt;/h1&gt;&lt;p&gt;Try it.&lt;p&gt;You&#39;ll totally&lt;br /&gt;love it.&lt;br /&gt;&lt;b&gt;Sample Bold text&lt;/b&gt;&lt;/body&gt;&lt;/html&gt;&#34;;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&#34;Welcome&#34;
                                                                     message:htmlString
                                                            preferredStyle:UIAlertControllerStyleAlert];
    NSAttributedString *attributedStr = [ initWithData:
               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                         NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
    documentAttributes:nil error:nil];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@&#34;OK&#34; style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
      // OK button tappped.
      ;
    }];
    ;
    ;
    ;
}
</code></pre>

<p>我包含了 viewDidAppear 声明,因为我在很长一段时间内都有一个错字,导致 UIAlertController 无法出现,因为整个函数从未被调用。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 UIAlertController 消息上显示 HTML,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41858762/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41858762/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 UIAlertController 消息上显示 HTML