How to Set Kerning (Spacing Between Characters) on Uinavigationbar Title - Swift or Objective-C

How to set kerning (spacing between characters) on UINavigationBar title - Swift or Objective-C

According to the documentation, the titleTextAttributes of UINavigationBar only lets you specify the font, text color, text shadow color, and text shadow offset.

If you want to use other attributes, you can create a UILabel with the NSAttributedString you want, and set it as the titleView for your controller's navigationItem

For example:

UILabel *titleLabel = [UILabel new];
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
NSKernAttributeName: @2};
titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.navigationItem.title attributes:attributes];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;

Justify text in iOS (Swift or Objective-C) without breaking tracking/kerning/letter spacing

Have you tried adding a NSParagraphStyle attribute with hyphenationFactor of 1?

This will keep your kerning and word spacing, but will add hyphenation when the word needs to break to the next line.

Kerning on a UIBarButtonItem with Swift

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

Change Character spacing in navigation bar title swift

You can not set attributed string directly.

You can do a trick by replacing titleView

let titleLabel = UILabel()
let colour = UIColor.redColor()
let attributes: [NSString : AnyObject] = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: colour, NSKernAttributeName : 5.0]
titleLabel.attributedText = NSAttributedString(string: "My String", attributes: attributes)
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel

Sample Image



Related Topics



Leave a reply



Submit