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 = @"<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 = [ initWithData:
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" 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]