UILabel - shrink text instead of truncating tail
Asked Answered
J

4

6

I have a UILabel set with a font of system bold 14.0, a minimum font size of 12. I want the label to fill 7 lines, and if it's too big, shrink the text down to 12 pixels, in which case it might be more than 7 lines, but still fit in it's original frame.

I've tried setting the number of lines to 7 and to 0. Either way the text just fills the 7 lines at the default size (14) and truncates the tail. How can I get the text to shrink down to 12px so that I can see more text?

(I would post more code, but most of these are set in IB).

EDIT: I have the Autoshrink option set to "Minimum Font Size" with a size of 12. Even if I set this to something obvious like 8, no shrinkage happens.

Jere answered 27/11, 2012 at 23:41 Comment(1)
You said you set the minimum font size of 12. How did you do that? Did you do that through IB in "Autoshrink" inspector properties of the UILabel?Cytology
B
5

Check with

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

[labelText sizeWithFont:labelFont 
                  minFontSize:12.0 
                  actualFontSize:&returnFontSize 
                  forWidth:frame.size.width 
                  lineBreakMode:UILineBreakModeWordWrap];

For more details check apple documentation for NSString. There are some other convenience methods also available for the NSString.

Update: Based on your edit, a work around for your issue is to set the following property,

label.adjustsFontSizeToFitWidth = YES;
Blondellblondelle answered 27/11, 2012 at 23:46 Comment(5)
This seems like something I should be able to do in IB. Why is there an Autoshrink option there if it doesn't do anything?Jere
Saw your edit on question now only. So you want to set it in IB. I thought you are looking for doing this programmatically.Blondellblondelle
@soleil, try setting your label.adjustsFontSizeToFitWidth = YES;Blondellblondelle
Tried that. No difference. What does Line Breaks need to be set to?Jere
UILineBreakModeWordWrap should be fine.Blondellblondelle
R
4

You can use .adjustsFontSizeToFitWidth=YES this will adjust the font size to fit the width of the label. For example, if you have a label with numberOfLines=2 and the text is too long, the font size of the label will be reduced to fit all the text.

Reiterate answered 16/2, 2015 at 11:31 Comment(1)
What will that do? Can you explain what that actually means?Mathematics
J
0

So apparently autoshrink, minimum font size, etc. only work in a 1-line UILabel. I found a solution here, although it would be nice if you could handle this in IB or in the UILabel API.

Jere answered 28/11, 2012 at 0:32 Comment(1)
That is by doing it programmatically. Similar to what I suggested. But in your case you should be using sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: since you want to set min font as 12 pixel as per your question.Blondellblondelle
C
0

For anyone still looking, it seems like the font size won't scale unless lineBreak = "Truncate Tail" I'm guessing the UILabel doesn’t know it needs to resize the text until it tries to truncate.

So to achieve the desired effect, use the following (with whatever font sizes and line count you wish):

Font Size: 14
Lines: 2
Automatically Adjusts Font Size: true
Auto Shrink: "Minimum font size"
Minimum Font Size: 8
Line Break: "Truncate Tail"

enter image description here enter image description here

Keep in mind that with this method if there is too much text for the view even at the minimum font size, it will eventually ellipsize the tail.

Celle answered 10/2, 2020 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.