【问题标题】:bar Button Item text Will change when select in moonIcon like Font Awesome in swift 4bar Button Item text 在moonIcon中选择时会发生变化,如swift 4中的Font Awesome
【发布时间】:2018-10-09 12:49:25
【问题描述】:

我使用此代码在我的应用程序中为 UIBarButtonItemnavigationController 中使用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


【解决方案1】:

这可能很简单,因为仅当按钮的状态为 .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 给出的答案类似)。

【讨论】:

  • 我收到此错误:无法将类型“[UIControlState]”的值转换为预期的参数类型“UIControlState”
  • 我用 UIControlState.ArrayLiteralElement(allControlStates) 而不是 [] 但又得到了同样的结果
【解决方案2】:
  1. 再次生成字体。
  2. 下载并复制正确的代码。
  3. 删除旧字体文件并将新字体文件 (.ttf) 添加到应用程序。
  4. 确保您已将字体包含在 info.plist 中的 Fonts provided by application 列表中。
  5. 使用新代码。

setTitleTextAttributes(<#T##attributes: [String : Any]?##[String : Any]?#>, for: <#T##UIControlState#>)

    let dictionary = [NSAttributedString.Key.font : UIFont(name: "icomoon", size: 25)!, NSAttributedString.Key.foregroundColor : UIColor.red]

    menuButton.setTitleTextAttributes(dictionary, for: .normal)
    menuButton.setTitleTextAttributes(dictionary, for: .disabled)
    menuButton.setTitleTextAttributes(dictionary, for: .highlighted)

【讨论】:

  • 再次收到相同的结果就像这里的图片
  • @SaeedRahmatolahi 你能尝试用一些字符串(例如菜单)替换publicSVGAssets().menu吗?
  • 这个变量是公共的,它等于这个公共的 var menu = "\u{e905}"
  • @SaeedRahmatolahi 你提到的代码是你得到的。
  • 我去了moonIcon网站并将我的图像转换为字体并在我的代码中使用它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
相关资源
最近更新 更多