Ivan
Ivan

Reputation: 515

UILabel how to tighten letter spacing

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...

Upvotes: 10

Views: 8030

Answers (3)

A Widera
A Widera

Reputation: 176

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

Upvotes: 9

Clay Bridges
Clay Bridges

Reputation: 11880

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;

Upvotes: 5

foundry
foundry

Reputation: 31745

You are probably looking for this:

@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth

Property of UILabel new in iOS6.

Upvotes: 18

Related Questions