Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize text to fit a label in Swift

Tags:

I want to display some text in a label.

The label has a fixed size but the text can vary from one single word to a complete sentence.

Is there a way to automatically resize the text in such a way that it is alway displayed in the largest possible font-size without truncating it?

I'm working through tutorials for days now, but can only find ways to adjust the label size, not the text size.

like image 501
John Allijn Avatar asked Apr 28 '15 08:04

John Allijn


People also ask

How do I resize a label in Xcode?

Here's the step that work in mine: Set width constraint to the label, then click the constraint. Select Size Inspector. Set the relation to less than or equal, and set max width.

How do I change the dynamic height of a label in Swift?

To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height. Let's create a label and add it as a subview to our view.


2 Answers

Set minimumScaleFactor property of UILabel.

How to set minimumScaleFactor

like image 99
Satachito Avatar answered Sep 27 '22 19:09

Satachito


You can change the font size to fit the width of the UILabel (non-multiline):

myLabel.adjustsFontSizeToFitWidth = true
like image 30
Erik Avatar answered Sep 27 '22 20:09

Erik