Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel - shrink text instead of truncating tail

Tags:

ios

uilabel

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.

like image 625
soleil Avatar asked Nov 27 '12 23:11

soleil


2 Answers

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;
like image 141
iDev Avatar answered Oct 28 '22 06:10

iDev


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.

like image 45
DevC Avatar answered Oct 28 '22 05:10

DevC