3

I have a UIBarButtonItem that I would like to the content text Kerned.
(I am setting the bar button item the same as the following: Add NSAttributedString to UIBarButtonItem)

Using that, in the attributes array, I add NSKernAttributeName but it doesn't seem to apply at all. I can change font size, font and so on, but the kerning never changes.

How do I correctly change the Kern values on the UIBarButton Text?

Community
  • 1
  • 1
daredevil1234
  • 1,303
  • 1
  • 10
  • 34

1 Answers1

2

The way you can do it is to initialize UIBarButtonItem with customView. The custom view should be 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
Irfan
  • 432
  • 1
  • 6
  • 17
  • 1
    Right, I never gave credit for this answer. it actually wasn't quite right, and I'd have to go back through that project's code to provide the correct way, but this was a major help in getting it working. sorry it took so long to give you answer credit. will do so now. – daredevil1234 Mar 23 '17 at 19:57