How do I set custom font to UIALertAction?

Viewed 2138

I know there are some related questions but they are either for changing font for title and message of the UIAlertController or very old (for Objective C mostly). I am using Swift 4.1

I am using this code and not sure how to set custom font for the UIAlert actions:

let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        let actionButton1 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

        let actionButton2 = UIAlertAction(title: "  Resend Code", style: .default) {
            action in
            self.resend()
        }

        actionButton2.setValue(UIImage(named: "MailForActionSheet"), forKey: "image")
        actionButton2.setValue(0, forKey: "titleTextAlignment")

        let actionButton3 = UIAlertAction(title: "  Call", style: .default, handler:
        {
            action in
            self.call()
        }
        )
        actionButton3.setValue(UIImage(named: "CallForActionSheet"), forKey: "image")
        actionButton3.setValue(0, forKey: "titleTextAlignment")


        actionSheet.addAction(actionButton1)
        actionSheet.addAction(actionButton2)
        actionSheet.addAction(actionButton3)

        present(actionSheet, animated: true, completion: nil)

Please help me understand how to set custom font to the text in actionButton1, actionButton2, actionButton3. What I intend to do is change the font style for these. Thank you

I have adding the following lines from https://stackoverflow.com/a/38746902/10375884 but I get the error: Value of type 'Any?' has no member 'value'

actionButton2.setValue(UIImage(named: "MailForActionSheet"), forKey: "image")
        actionButton2.setValue(0, forKey: "titleTextAlignment")

        let attributedText = NSMutableAttributedString(string: "  Resend Code")
        let range = NSRange(location: 0, length: attributedText.length)
        attributedText.addAttribute(NSAttributedStringKey.kern, value: 1.5, range: range)
        attributedText.addAttribute(NSAttributedStringKey.kern, value: UIFont(name: "SFProText-Semibold", size: 20.0)!, range: range)

actionSheet.addAction(actionButton2)

guard let label = actionButton1.value(forKey: "__representer")?.value(forKey: "label") as? UILabel else { return }
        label.attributedText = attributedText
0 Answers
Related