在UIAlertController消息上显示HTML

时间:2017-01-25 18:18:00

标签: ios swift uialertcontroller

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

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

3 个答案:

答案 0 :(得分:0)

您可以使用UIAlertController属性访问textFields的文本字段。然后,您可以通过设置attributedText来设置包含HTML的属性字符串。

答案 1 :(得分:0)

好吧,当rmaddy sugested时,我已经使用ViewController创建了一个自定义提醒。

我在StoryBoard中添加了一个ViewController,不透明度为75%。在它上面,有两个标签(警报和消息)和一个按钮(关闭整个事物)的视图。在关联的类中,唯一的代码是:

var alertTitle: String?
var messageContent: String?

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var messageLabel: UILabel!

@IBOutlet weak var AlertView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    AlertView.layer.cornerRadius = 5.0

    let attrStr = try! NSAttributedString(
        data: (messageContent?.data(using: String.Encoding.unicode, allowLossyConversion: true)!)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)

    titleLabel.text = alertTitle

    messageLabel.lineBreakMode = .byWordWrapping
    messageLabel.numberOfLines = 0
    messageLabel.attributedText = attrStr
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}





@IBAction func closeButtonTapped(_ sender: Any) {

    self.dismiss(animated: true, completion: nil)
}

基本上,这将收到将显示为标题的文本和消息(将是HTML)。

要使用该ViewController,我使用以下代码创建了一个函数:

func createAlert(title: String, message: String) {

    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let alert = storyBoard.instantiateViewController(withIdentifier: "alert") as! AlertViewController

    alert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    alert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve

    alert.alertTitle = title
    alert.messageContent = message

    self.present(alert, animated: true, completion: nil)
}

据我所知,它有效。消息看起来像没有样式的纯HTML文本(所以我需要纠正它),但它做了我需要的。

有点奇怪,iOS上的Alerts无法显示来自HTML的内容。我认为它可以是一个很好的功能。无论如何,感谢rmaddy的这种消化。这比我预想的要容易。

答案 2 :(得分:0)

我不想在我的项目中添加更多的类,因此我一直在寻找一种更直接的方法来实现。我发现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不会出现。

相关问题