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
Related Topics
Swift: Protocol VS. Struct VS. Class
API to Capture Live Photos in iOS9
Receipt Validation on iOS In-App-Purchase Returns Multiple Transaction
Exc_Bad_Access Using Ibinspectable
How to Make Synchronous Operation with Asynchronous Callback
Programmatically Set Uipageviewcontroller Transition Style to Scroll
How Is This Slide-Up Menu from the iPhone Messages App Implemented
Hls Metadata Id3 Tag Not Working
Handle Multiple File (Image) Uploads to Aws S3 Swift
7 Duplicate Symbols for Architecture X86_64 in React Native
App Groups Forsecurityapplicationgroupidentifier Returns Nil
Output Video Size Huge Using Hevc Encoder on iOS
How to Download a PDF File in Swift and Find in File Manager