如何使用Swift更改iOS警报消息框标题和消息中的字体大小

问题描述 投票:1回答:1

我想用Swift更改消息框标题和消息的字体大小。 我不想要字体颜色或字体系列。我只是想改变字体大小和行高。

let attributedString = NSAttributedString(string: "Title", attributes: [
NSFontAttributeName : UIFont.systemFontOfSize(15) //your font here,
NSForegroundColorAttributeName : UIColor.redColor()])
let alert = UIAlertController(title: "", message: "",  preferredStyle: .Alert)

alert.setValue(attributedString, forKey: "attributedTitle")
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}

presentViewController(alert,
animated: true,
completion: nil)
swift uialertview font-size uialertcontroller
1个回答
-1
投票

你需要改变它的关键

请检查以下代码:

 let alert = UIAlertController(title: "Test Title", message: "Custom Fonts in Alert Controller", preferredStyle: .actionSheet)
    let strAction = NSMutableAttributedString(string: "Presenting the great...")
    strAction.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 30), range: NSMakeRange(24, 11))
    alert.setValue(action, forKey: "attributedMessage")// change key name if you want to change title font with attributedTitle

    let action = UIAlertAction(title: "Some title", style: .default, handler: nil)
    alert.addAction(action)
    present(alert, animated: true, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.