使用 Swift 对 UIBarButtonItem 进行字距调整

Kerning on a UIBarButtonItem with Swift

我有一个 UIBarButtonItem,我希望它的内容文本紧缩。
(我将条形按钮项设置为与以下相同:Add NSAttributedString to UIBarButtonItem

使用它,在属性数组中,我添加了 NSKernAttributeName 但它似乎根本不适用。我可以更改字体大小、字体等,但字距不会改变。

如何正确更改 UIBarButton 文本上的 Kern 值?

您可以使用 customView 初始化 UIBarButtonItem。自定义视图应该是 UIButton。

     let button  = UIButton(type: .Custom)

     let attributes = [
        NSFontAttributeName: UIFont.boldSystemFontOfSize(12),
        NSForegroundColorAttributeName: UIColor.redColor(),
        NSKernAttributeName: CGFloat(1.7)
    ]
    button.frame = CGRectMake(0.0, 0.0, 60, 35)

    let attributedTitle = NSAttributedString(string: "CLOSE", attributes: attributes)
    button.setAttributedTitle(attributedTitle, forState: .Normal)
    button.addTarget(self, action: #selector(Controller.leftBarButtonItemSelected), forControlEvents: .TouchUpInside)
    let barButton = UIBarButtonItem.init(customView: button)
    navigationItem.leftBarButtonItem = barButton