What's the default kerning for UILabel (attributed text looks different)
According to the documentation for the UILabel attributed text property
To turn on auto-kerning on the label, set kern of the string to null.
Looks like auto-kerning for plain text is on by default, for attributed text is off by default. Here's how to turn auto-kerning back on for the attributed text property:
let label = UILabel()
let attributes = [NSAttributedString.Key.kern: kCFNull!]
label.attributedText = NSMutableAttributedString(string: "Text", attributes: attributes)
iOS/Objective-C UILabel text kerning
You want to update your attributedText
every time kerning is updated. So, your .h should look like:
IB_DESIGNABLE
@interface KerningLabel : UILabel
@property (nonatomic) IBInspectable CGFloat kerning;
@end
and your .m :
@implementation KerningLabel
- (void)setKerning:(CGFloat)kerning
{
_kerning = kerning;
if(self.attributedText)
{
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
[attribString addAttribute:NSKernAttributeName value:@(kerning) range:NSMakeRange(0, self.attributedText.length)];
self.attributedText = attribString;
}
}
@end
How do I change the letter spacing in a UILabel?
You can use the NSKernAttributeName
attribute on an attributed string:
UILabel *label = [UILabel new];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
initWithString:@"127"];
// The value paramenter defines your spacing amount, and range is
// the range of characters in your string the spacing will apply to.
// Here we want it to apply to the whole string so we take it from 0 to text.length.
[text addAttribute:NSKernAttributeName
value:@-0.5
range:NSMakeRange(0, text.length)];
[label setAttributedText:text];
How to increase the character spacing in UILabel
You should probably use NSAttributedString
with NSKernAttributeName
attribute
Here is a small example:
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
NSString *string = @"Some important text";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
float spacing = 5.0f;
[attributedString addAttribute:NSKernAttributeName
value:@(spacing)
range:NSMakeRange(0, [string length])];
label.attributedText = attributedString;
[self.view addSubview:label];
How to change an UILabel/UIFont's letter spacing?
I've come up with a solution for the letter spacing and the alignment to the right.
Here it goes:
NSString *number = [NSString stringWithFormat:@"%d", total];
int lastPos = 85;
NSUInteger i;
for (i = number.length; i > 0; i--)
{
NSRange range = {i-1,1};
NSString *n = [number substringWithRange:range];
UILabel *digit = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 35, 50)] autorelease];
digit.text = n;
digit.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
digit.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
digit.backgroundColor = [UIColor clearColor];
[self addSubview:digit];
CGSize textSize = [[digit text] sizeWithFont:[digit font]];
CGFloat textWidth = textSize.width;
CGRect rect = digit.frame;
rect.origin.x = lastPos - textWidth;
digit.frame = rect;
lastPos = rect.origin.x + 10;
}
The letter spacing is the "10" on the last line.
The alignment comes from the lastPos.
Hope this helps anyone out there.
Change character spacing on UILabel within Interface Builder
Ended up using this for now to get existing attributed text and modify to add character spacing:
let attributedString = discoveryTitle.attributedText as NSMutableAttributedString
attributedString.addAttribute(NSKernAttributeName, value: 1.0, range: NSMakeRange(0, attributedString.length))
discoveryTitle.attributedText = attributedString
Swift 3:
let attributedString = NSMutableAttributedString(string: discoveryTitle.text)
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.0), range: NSRange(location: 0, length: attributedString.length))
discoveryTitle.attributedText = attributedString
Using NSRange instead of NSMakeRange works in Swift 3.
How do you adjust text kerning using Interface Builder in Xcode 7?
You can actually do this without the use of a subclass through an extension.
import UIKit
@IBDesignable
extension UILabel {
@IBInspectable
public var kerning:CGFloat {
set{
if let currentAttibutedText = self.attributedText {
let attribString = NSMutableAttributedString(attributedString: currentAttibutedText)
attribString.addAttributes([NSKernAttributeName:newValue], range:NSMakeRange(0, currentAttibutedText.length))
self.attributedText = attribString
}
} get {
var kerning:CGFloat = 0
if let attributedText = self.attributedText {
attributedText.enumerateAttribute(NSKernAttributeName,
in: NSMakeRange(0, attributedText.length),
options: .init(rawValue: 0)) { (value, range, stop) in
kerning = value as? CGFloat ?? 0
}
}
return kerning
}
}
}
While this won't actually show up in interface builder it will show up and work when you run your app.
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;
Related Topics
What Is a "Delegate" in Objective C's iPhone Development
Uibezierpath Triangle with Rounded Edges
Use Didselectrowatindexpath or Prepareforsegue Method for Uitableview
Native Facebook App Does Not Open with Facebook Login in iOS 9
Change Color of Back Button in Navigation Bar
Analysing Assets.Car File in iOS
Why Is Uicollectionviewcell's Outlet Nil
iPhone Ad Hoc Build Using Xcode 4
Ios: Uibutton Resize According to Text Length
How to Change Text on a Back Button
iOS Autolayout with Uiscrollview: Why Does Content View of Scroll View Not Fill the Scroll View
How to Display a Uipopoverview as a Annotation to the Map View? (iPad)
Difference Between _ and Self. in Objective-C
Showing Pushviewcontroller Animation Look Like Presentmodalviewcontroller
iPhone Read Uiimage (Frames) from Video with Avfoundation