我知道有一些相关问题,但是它们要么用于更改UIAlertController的标题和消息的字体,要么用于更改旧字体(主要用于Objective C)。我正在使用Swift 4.1
我正在使用此代码,不确定如何为UIAlert操作设置自定义字体:
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)
请帮助我了解如何为actionButton1, actionButton2, actionButton3
中的文本设置自定义字体。我打算做的就是更改这些字体的样式。
谢谢
我在https://stackoverflow.com/a/38746902/10375884中添加了以下几行,但出现错误: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