Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel how to tighten letter spacing

Tags:

ios

uilabel

I would like to know how to programatically "Tighten Letter Spacing"?

This option is available for a UILabel made in a xib file, and is really handy sometimes.

I know this question has been asked before, but I don't see an answer that mentions this beeing available in the interface builder, so I was curious...

like image 341
Ivan Avatar asked Feb 12 '13 11:02

Ivan


3 Answers

You might want to use UILabel property:

allowsDefaultTighteningForTruncation: Bool

and set it to true. (Default is false)

According to Apple Documentation:

When this property is set to true, the label tightens intercharacter spacing of its text before allowing any truncation to occur. The label determines the maximum amount of tightening automatically based on the font, current line width, line break mode, and other relevant information.

https://developer.apple.com/reference/uikit/uilabel/1620533-allowsdefaulttighteningfortrunca

like image 104
A Widera Avatar answered Oct 08 '22 15:10

A Widera


You are probably looking for this:

@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth

Property of UILabel new in iOS6.

like image 36
foundry Avatar answered Oct 08 '22 13:10

foundry


adjustsLetterSpacingToFitWidth is deprecated as of iOS 7.

You'd now (as of iOS 8) probably want to do something like:

NSAttributedString *as = 
[[NSAttributedString alloc] initWithString:@"Kerninating all the strings"
                                attributes:@{NSKernAttributeName : @(-2.0)}];
label.attributedText = as;
like image 21
Clay Bridges Avatar answered Oct 08 '22 14:10

Clay Bridges