backslash112
backslash112

Reputation: 2240

How to limit max width of UILabel?

There are a UILabel in the center of the UIView, named nicknameLabel, and will append a gender image after the nicknameLabel.

But when the nicknameLabel has a lot of text, it will beyond the bounds of the UIView.

enter image description here

So, how to limit max width of UILabel?

BTW: I using the storyboard. Thanks.

Upvotes: 18

Views: 15900

Answers (3)

backslash112
backslash112

Reputation: 2240

Give the label a <= constraint like this:

enter image description here

Upvotes: 41

abhishekkharwar
abhishekkharwar

Reputation: 3529

If you are using constraints then remove all constraints from you label.

enter image description here

Then again set frames of your label and if you want to make multiline label, then simply set the number of lines from 1 to 0 and increase the height of your label.

enter image description here

OR

If you want to fit your text within the label.You can simply enable Autoshrink property.

enter image description here

Upvotes: 2

Abhishek Sharma
Abhishek Sharma

Reputation: 3283

use following code and set your width in boundingRectWithSize value.

CGSize itemTextSize = [@"Your Text" boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica Neue" size:12.5]} context:nil].size;

Upvotes: 4

Related Questions