【问题标题】:Kerning on a UIBarButtonItem with Swift使用 Swift 调整 UIBarButtonItem 的字距
【发布时间】:2016-01-18 20:17:33
【问题描述】:

我有一个 UIBarButtonItem 我想要内容文本 Kerned。
(我设置栏按钮项如下:Add NSAttributedString to UIBarButtonItem

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

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

【问题讨论】:

    标签: ios swift swift2 uibarbuttonitem


    【解决方案1】:

    您可以这样做的方法是使用 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
    

    【讨论】:

    • 是的,我从来没有对这个答案表示赞赏。它实际上并不完全正确,我必须返回该项目的代码以提供正确的方法,但这是使其正常工作的主要帮助。抱歉,花了这么长时间才给你答案。现在会这样做。
    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2015-06-07
    相关资源
    最近更新 更多