ios - bar Button Item text 在moonIcon中选择时会发生变化,如swift 4中的Font Awesome
问题描述
我使用此代码在我的应用程序中使用moonIcon Font like font UIBarButtonItem
AwesomenavigationController
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
解决方案
它可能很简单,因为仅在按钮状态为.normal
(如您指定)时才应用字体属性。
您可能需要指定按钮的所有状态都应用这些标题文本属性,如下所示:
let title = publicSVGAssets().menu
let style = UIBarButtonItemStyle.plain
let selector = #selector(menuAction)
let menuButton = UIBarButtonItem(title: title, style: style, target: self, action: selector)
let font = UIFont(name: "icomoon", size: 25)
let attributesDictionary: [NSAttributedStringKey: Any]? = [NSAttributedStringKey.font: font!, NSAttributedStringKey.foregroundColor : UIColor.red]
menuButton.setTitleTextAttributes(attributesDictionary, for: .normal)
menuButton.setTitleTextAttributes(attributesDictionary, for: .selected)
menuButton.setTitleTextAttributes(attributesDictionary, for: .highlighted)
self.navigationItem.rightBarButtonItem = menuButton
点击按钮将改变其状态。这可能就是您点击它时看到它更改图标的原因。
注意:如果您需要在其他状态下显示按钮(例如disabled
),您还需要为该状态分配这些相同的标题文本属性。
...例如:
menuButton.setTitleTextAttributes(attributesDictionary, for: .disabled)
(注意:这与@Satish 给出的答案类似)。
推荐阅读
- php - 如何在返回语句中获取“for or foreach”循环值作为回显语句获取
- c# - 在异步库方法中处理共享成员变量的推荐做法是什么?
- pyspark - Pyspark:如何为 df 中的某些选定列创建联合表
- android - 如何让android通知图片变大而不是像Youtube App这样的小徽章?
- .net-core - CentOS 上 Bamboo 中的 .Net Core SFTP 集成测试
- javascript - Javascript React:按下按钮时按钮不执行功能
- c++ - 如何解决 UndefinedBehaviorSanitizer 错误?
- c# - 如何在 C# 中将嵌套的 foreach 循环转换为 LINQ 查询
- c++ - 如何为动画关键帧添加权重
- swift - 如何告诉 Swift 使用像素或点?