Add NSAttributedString to UIBarButtonItem

南楼画角 提交于 2019-12-10 17:33:22

问题


I'm attempting to set an attributed string on my back bar button item.
This is my first attempt at attributed strings.
Here is the code:

    self.navigationItem.hidesBackButton = true
    let barButtonBackStr = "< Back"
    var attributedBarButtonBackStr = NSMutableAttributedString(string: barButtonBackStr as String)
    attributedBarButtonBackStr.addAttribute(NSFontAttributeName,
        value: UIFont(
            name: "AmericanTypewriter-Bold",
            size: 18.0)!,
        range: NSRange(
            location:0,
            length:1))
    let newBackButton = UIBarButtonItem(title: attributedBarButtonBackStr, style: UIBarButtonItemStyle.Plain, target: self, action: "barButtonBack:")
    self.navigationItem.leftBarButtonItem = newBackButton

This results in the following error in Xcode.

Cannot invoke initializer for type 'UIBarButtonItem' with an argument list of type '(title: NSMutableAttributedString, style: UIBarButtonItemStyle, target: CombatOutcomeViewController, action: String)'

Anyone have an idea on how to do this? Thanks.


回答1:


You cannot directly set an attributed string to an UIBarButtonItem. You have to set a normal string to its title and then set the attributes for the title:

let barButtonBackStr = "< Back"
let attributes: [String: AnyObject] = [NSFontAttributeName: UIFont(name: "AmericanTypewriter-Bold", size: 18)!]
let newBackButton = UIBarButtonItem(title: barButtonBackStr, style: UIBarButtonItemStyle.Plain, target: self, action: "barButtonBack:")
newBackButton.setTitleTextAttributes(attributes, forState: .Normal)
navigationItem.leftBarButtonItem = newBackButton

This approach has one caveat: You cannot set a range for the attributes. It's all or nothing.

To define a range for the attributes you have to create a UILabel, set an attributed string to its attributedText property and then create a UIBarButtonItem with a custom view:

let barButtonBackStr = "< Back"
let attributedBarButtonBackStr = NSMutableAttributedString(string: barButtonBackStr as String)
attributedBarButtonBackStr.addAttribute(NSFontAttributeName,
    value: UIFont(
        name: "AmericanTypewriter-Bold",
        size: 18.0)!,
    range: NSRange(
        location:0,
        length:1))
let label = UILabel()
label.attributedText = attributedBarButtonBackStr
label.sizeToFit()
let newBackButton = UIBarButtonItem(customView: label)
self.navigationItem.leftBarButtonItem = newBackButton

When you want to use this approach you have to know you have to set the target and the action to the custom view, because the UIBarButtonItem does not longer handle it. As it says in Apple's docs:

The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.



来源:https://stackoverflow.com/questions/33109309/add-nsattributedstring-to-uibarbuttonitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!