【发布时间】:2018-10-09 12:49:25
【问题描述】:
我使用此代码在我的应用程序中为 UIBarButtonItem 在navigationController 中使用moonIcon Font like font Awesome
let menuButton = UIBarButtonItem(title: publicSVGAssets().menu, style: UIBarButtonItemStyle.plain, target: self, action: #selector(menuAction))
menuButton.setTitleTextAttributes(NSDictionary(dictionary: [NSAttributedStringKey.font : UIFont(name: "icomoon", size: 25)!, NSAttributedStringKey.foregroundColor : UIColor.red]) as? [NSAttributedStringKey : Any], for: [])
self.navigationItem.rightBarButtonItem = menuButton
【问题讨论】:
-
考虑使用
let常量来包含您需要的参数值,以便进入menuButton构造方法和setTitleTextAttributes方法的参数。在尝试调试时,这将为您提供帮助。然后,您可以逐行执行let常量分配,并确保每个值都是正确的,然后再将它们插入构造函数和文本属性方法。 (有关此示例,请参见下面的答案)。在我看来,它的代码看起来也会更干净。
标签: ios swift4 font-awesome uibarbuttonitem uibarbuttonitemstyle