cyanide
cyanide

Reputation: 3964

ios: scaling font horizontally without rasterising

Android provides a property textScaleX, that allows to scale text horizontally while retaining font height. I use it quite extensively for Android apps (especially for languages which are not as compact as English) and wish to find something similar in ios.

Obviously, I can draw a text to a graphic context and scale it as an image, but this seems to be a rather awkward solution which turns all text items into ImageView-s.

Is there a native way to scale text horizontally? For example there might be an option to create a custom font based on a system font and change a parameter responsible for text width.

Upvotes: 0

Views: 208

Answers (2)

Kurt Revis
Kurt Revis

Reputation: 27984

Use the matrix attribute of UIFontDescriptor.

Here's an example that takes a UILabel in self.label and sets its font to be stretched horizontally by a factor of 1.3.

UIFont* baseFont = self.label.font;
UIFontDescriptor *baseDescriptor = baseFont.fontDescriptor;
UIFontDescriptor *newDescriptor = [baseDescriptor fontDescriptorWithMatrix:
    CGAffineTransformMakeScale(1.3, 1.0)];
UIFont* newFont = [UIFont fontWithDescriptor:newDescriptor size:0.0];
self.label.font = newFont;

Upvotes: 2

Nils Ziehn
Nils Ziehn

Reputation: 4331

You can just scale the UILabel or UITextView that contains the text:

UILabel *label = ...
label.transform = CGAffineTransformMakeScale(0.8,1); //reduced size in x direction by 20%

If that is not enough, you could look into NSLayoutManger, there may be something there.

Upvotes: 0

Related Questions