【问题标题】:Show HTML on UIAlertController message在 UIAlertController 消息上显示 HTML
【发布时间】:2017-01-25 18:18:00
【问题描述】:

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

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

【问题讨论】:

  • UIAlertController 不支持此类自定义。创建您自己的警报或查找第三方警报。
  • 谢谢。我创建了一个自定义警报并且它有效(它需要一些样式调整,但它有效)

标签: ios swift uialertcontroller


【解决方案1】:

我不想在我的项目中添加更多的类,所以我一直在寻找一种更直接的方法来做到这一点。我发现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 无法出现,因为整个函数从未被调用。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 代码片段对您已经很好的答案非常有帮助。
    • 访问文本字段如何让您将警报消息更改为 HTML?
    • @rmaddy,属性字符串支持 HTML 标记,因此通过使用这些,您可以在标签中呈现 HTML。
    • 我了解属性字符串和 HTML。那不是我的意思。您建议使用警报的文本字段来显示消息。
    【解决方案3】:

    好吧,正如 rmaddy 所建议的,我已经使用 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 上的警报无法显示 HTML 内容,这有点奇怪。我认为这可能是一个不错的功能。无论如何,感谢 rmaddy 的建议。这比我预期的要容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多