如何访问和更改由 UIButton 组成的自定义 barButtonItem 的 NSAttributedString 标题?
How to access and change the NSAttributedString title of a custom barButtonItem composed by UIButton?
我在工具栏中创建了一个自定义 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我也可以看。
欢迎任何建议。谢谢
有没有人发现Whosebug可以当小黄鸭用?哈哈
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()
}
您可以像这样使用自定义按钮操作方法
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)
更改目标代码如下。
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)
}
我在工具栏中创建了一个自定义 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我也可以看。 欢迎任何建议。谢谢
有没有人发现Whosebug可以当小黄鸭用?哈哈
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()
}
您可以像这样使用自定义按钮操作方法
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)
更改目标代码如下。
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)
}