I would like to know if there's a way to show HTML in the message of a UIAlertController.
Android has a way to do it, but I cannot find a way to do the same on iOS. I'm using Swift 3 right now.
I would like to know if there's a way to show HTML in the message of a UIAlertController.
Android has a way to do it, but I cannot find a way to do the same on iOS. I'm using Swift 3 right now.
I didn't want to add any more classes to my project, so I kept looking for a more direct way to do it. I found this link very helpful.
My final code looks like this:
-(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];
}
I included the viewDidAppear declaration because I had a typo in mine for a long time which kept the UIAlertController from appearing because the whole function was never being called.
You can access the text fields of UIAlertController
using the textFields
property. Then you can set an attributed string containing HTML going through the text fields by setting attributedText
.
Well, as rmaddy sugested, i've created a custom alert using a ViewController.
I've added a ViewController in the StoryBoard, with 75% opacity. Above it, a view with two labels (alert and message) and a button (to close the whole thing). In the associated class, the only code will be:
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)
}
Basically, this will receive the text that will be shown as title, and the message (which would be HTML).
To use that ViewController, i created a function with this code:
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)
}
As far i can tell, it works. Message looks like pure HTML text without style (so i will need to correct that), but it does what i needed.
It's a little strange that Alerts on iOS have not a way to show content from HTML. I think that it can be a nice feature. Anyway, thanks rmaddy for this sugestion. It was easier than i expected.