本文介绍了将 NSAttributedString 添加到 UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在后栏按钮项上设置属性字符串.
这是我第一次尝试使用属性字符串.
代码如下:

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

这会导致 Xcode 中出现以下错误.

This results in the following error in Xcode.

无法使用参数调用类型UIBarButtonItem"的初始化程序类型列表'(标题:NSMutableAttributedString,样式:UIBarButtonItemStyle,目标:CombatOutcomeViewController,动作:字符串)'

有人知道如何做到这一点吗?谢谢.

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

推荐答案

您不能直接将属性字符串设置为 UIBarButtonItem.您必须为其标题设置一个普通字符串,然后设置标题的属性:

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: [NSAttributedString.Key : AnyObject] = [.font : UIFont(name: "AmericanTypewriter-Bold", size: 18)!]
let newBackButton = UIBarButtonItem(title: barButtonBackStr, style: .plain, target: self, action: "barButtonBack:")
newBackButton.setTitleTextAttributes(attributes, for: .normal)
navigationItem.leftBarButtonItem = newBackButton

这种方法有一个警告:您不能为属性设置范围.要么全有,要么全无.

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

要定义属性范围,您必须创建一个 UILabel,将属性字符串设置为其 attributedText 属性,然后创建一个 UIBarButtonItem> 带有自定义视图:

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(NSAttributedString.Key.font,
    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

当你想使用这种方法时,你必须知道你必须将目标和动作设置为自定义视图,因为 UIBarButtonItem 不再处理它.正如 Apple 文档中所说:

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:

此方法创建的条形按钮项不调用动作其目标响应用户交互的方法.相反,条形按钮项期望指定的自定义视图来处理任何用户互动并提供适当的回应.

这篇关于将 NSAttributedString 添加到 UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:07