【问题标题】:How to access and change the NSAttributedString title of a custom barButtonItem composed by UIButton?如何访问和更改由 UIButton 组成的自定义 barButtonItem 的 NSAttributedString 标题?
【发布时间】:2016-11-11 16:09:10
【问题描述】:

我在我的工具栏中制作了一个自定义 barButtonItem。我想在点击 barButtonItem 时更改 NSAttributedString 的字符串。但是正如苹果文档所说,该功能的性能是由customView负责的,而不是barButtonItem,我怎样才能根据事件访问并对其细节进行一些更改?

    let button1 = UIButton(type: .System)
    button1.sizeToFit()
    let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
    let attributedString1 = NSAttributedString(string: "Hello", attributes: attributes1)
    button1.setAttributedTitle(attributedString1, forState: .Normal)
    button1.addTarget(self, action: #selector(ActionViewController.switchAccounts), forControlEvents: .TouchUpInside)
    let account = UIBarButtonItem(customView: button1)

    setToolbarItems([account], animated: true)

我是一个新的迅捷者。 OC我也可以读。 欢迎任何建议。谢谢。

【问题讨论】:

    标签: ios swift uibutton uibarbuttonitem


    【解决方案1】:

    有没有人发现stackoverflow可以当小黄鸭?哈哈

    for item in toolbarItems! where item.tag == 1000 {
            let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
            let attributedString1 = NSAttributedString(string: "world!", attributes: attributes1)
    
            let button = item.customView as! UIButton
            button.setAttributedTitle(attributedString1, forState: .Normal)
            button.sizeToFit()
        }
    

    【讨论】:

      【解决方案2】:

      您可以像这样使用自定义按钮操作方法

      func switchAccounts(sender : UIButton)  {
          let attributes1 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
          let attributedString1 = NSAttributedString(string: "world!", attributes: attributes1)
      
          sender.setAttributedTitle(attributedString1, forState: .Normal)
          sender.sizeToFit()
      }
      

      button1.addTarget(self, action: #selector(ActionViewController.switchAccounts(_:))
      

      而不是

      button1.addTarget(self, action: #selector(ActionViewController.switchAccounts)
      

      【讨论】:

      • 谢谢。这终于是我的解决方案了。
      【解决方案3】:

      如下更改目标代码。

      button1.addTarget(self, action: #selector(ActionViewController.switchAccounts(_:)), forControlEvents: .TouchUpInside)
      

      使 switchAccounts() 如下所示。

      func switchAccounts(sender:UIButton){
          sender.selected = !sender.selected
          let attributes1 = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(13)]
          let attributedString1 = NSAttributedString(string: "Hello click", attributes: attributes1)
          sender.setAttributedTitle(attributedString1, forState: .Selected)
      }
      

      【讨论】:

      • 谢谢。这终于是我的解决方案了。
      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多