3

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
  • Possible duplicate of https://stackoverflow.com/questions/52095864/change-uialertcontrollers-title-fontsize/52096904#52096904 – Sunny Sep 21 '18 at 07:25
  • Check question and accepted answer again. It changes font of `UIAlertAction` also. It changes font of action title as well. – Sunny Sep 21 '18 at 07:28
  • @Sunny, I want to use custom font height and style, not one of those predefined style. –  Sep 21 '18 at 07:31
  • You can using this answer https://stackoverflow.com/a/27518769/2776008. Use a private APIs can make your app rejected by Apple – Quoc Nguyen Sep 21 '18 at 08:50
  • @QuocNguyen, its objective C. I don't understand what they mean. Can you help me understand how to do the same for Swift? –  Sep 21 '18 at 09:07
  • Look at here [We can apply custom fonts to **AlertView** and **AlertAddAction**](https://stackoverflow.com/questions/37744237/how-to-edit-uialertaction-text-font-size-and-color/63889558#63889558) – Sreekanth G Sep 14 '20 at 18:03

0 Answers0