How does a UILabel's minimumScaleFactor work? - ios

I have used minimumFontSize before but that function is now deprecated and i don't quite understand how minimumScaleFactor works.
I want the maximum font size to be 10 and the minimum to be 7.
How can I achieve the re-size down to font size 7 with the scale factor?
UILabel creation:
UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
label.text = [labelName uppercaseString];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:9.5];
label.backgroundColor = [UIColor clearColor];
label.minimumScaleFactor = .1f;
[label addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[label(WIDTH)]"
options:0
metrics:#{#"WIDTH" : [NSNumber numberWithFloat:buttonSize.width]}
views:NSDictionaryOfVariableBindings(label)]];
[contentView addSubview:label];

You need to set the label.adjustsFontSizeToFitWidth = YES;

In addition to what the other answers say, if you put minSize/defaultSize (division) as the minimumScaleFactor, it will be the same as using the old minimumFontSize.
Ex, if you want the minimum font size to be 10 using default label size, you can do:
[label setMinimumScaleFactor:10.0/[UIFont labelFontSize]];
(Replace [UIFont labelFontSize] with your label's font size if it is not the default).
which would be the same as:
[label setMinimumFontSize:10.0];

According to the documentation:
Use this property to specify the smallest multiplier for the current font size that yields an acceptable font size to use when displaying the label’s text. If you specify a value of 0 for this property, the current font size is used as the smallest font size.
So if default font size for your label is 10, you put 0.7f as a minimumScaleFactorand it should do the same thing as minimumFontSize did.

In addition to other answers, I'm going to add a beginner-friendly explanation that helped myself:
How to calculate a minimumScaleFactor? Divide your label's minimum font size by your label's default font size. For example, your default font size is 25. Your minimum font size is 10.
10/25 = 0.4
0.4 is your minimumScaleFactor value. Also see #Jsdodgers's answer above.

Related

How to get the current font size of an UILabel

I have adjusted the UILabel font size according to the width in this way.
[btnPending.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:15]];
[btnPending setBackgroundColor:[UIColor clearColor]];
btnPending.titleLabel.adjustsFontSizeToFitWidth = YES;
btnPending.titleLabel.numberOfLines = 1;
btnPending.titleLabel.lineBreakMode = NSLineBreakByClipping;
[_vwTabBtnParent addSubview:btnPending];
And its working fine. But now I want to get the current font size of that UILabel. How can I do this in objective-c.
Please help me,
Thanks
Then to get the font name and size all you need is
NSString *fontName = self.label.font.fontName;
CGFloat fontSize = self.label.font.pointSize;
Try below method:
[label.text sizeWithFont:label.font
minFontSize:label.minimumFontSize
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
Note: Deprecated in iOS 7 with no alternative.

UILabel don't adjust in height to fit the text

I know there is a lot of post about UILabel and it's size but i was unable to find a answer for my problem.
I have some UILabels with a text on 1 line, no more and don't want more lines, and the text is set to a minimum font scale to adjust. In portrait mode, the screen has enough space in heightand don't scale my UILabel in height to a critical point.
In landscape mode, i'm short in height and the UILabel is very small in height and the text is cut on the top and bottom.
I have tested everyting i have read like:
set line breaks to clip mode
use adjustsFontSizeToFitWidth = YES;
In every case, the text fit only in width but don't care about height.
Should i use some ugly hack to calcul the font size depending on the label height myself or have i miss something ?
CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];
CGFloat fontSize = 0.0f;
labelFrame.size = [myLabel.text sizeWithFont:myLabel.font
minFontSize:myLabel.minimumFontSize
actualFontSize:&fontSize
forWidth:labelFrame.width
lineBreakMode:myLabel.lineBreakMode];
myLabel.frame = labelFrame;
[self.view addSubview:myLabel];

UILabel Size to fit is not working

This is what I'm doing. I'm not dynamically creating the label, just picking from the object library.it is working only if I statically increase the height of the label, but I doesn't know at runtime
[self.lblDescription setText:#"This is going to be a test description but a very big description that should increase in height"];
[self.lblDescription sizeToFit];
The label is not going on the next line. But instead it is clipping it as soon as the left of the label reaches the right of the screen
Based on the answers and I tried the following but didn't work:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.lblDescription setText:#"This is going to be a test description but a very big description that should increase in size"];
self.lblDescription.numberOfLines = 0;
[self.lblDescription sizeToFit];
}
I also tried setting -
[self.lblDescription setLineBreakMode:NSLineBreakByWordWrapping];
before calling [self.lblDescription sizeToFit]; but didn't work
Setting adjustsFontSizeToFitWidth property to true did it for me.
Try,
self.lblDescription.adjustsFontSizeToFitWidth = true
(by default it is set to false)
Check for the numberOfLines property
self.lblDescription.numberOfLines = 0;
EDIT : Need to change the height as well (By the comment says) .
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica Neue" size:19], NSFontAttributeName,
nil];
CGRect expectedLabelFrame = [text boundingRectWithSize:CGSizeMake(571, 500)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
// Adjust the yourLabel the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelFrame.size.height;
yourLabel.frame = newFrame;
I've found that when using sizeToFit with a UILabel, in InterfaceBuilder you need to change the Autoshrink property from 'Fixed Font Size' to 'Minimum Font Size'. I usually then set its value to 0.5 to be sure its working properly.

Width of UILabel created programmatically

I'm trying to create a UILabel programmatically that fits it's content.
The best I could do was to use the NSString's method sizeWithAtributes to get it's content's required width and then apply a constraint that fixes the width to that value.
Is this the way it is supposed to be done using autolayout?
if you need some with aoutolayouts, i try comment:
UILabel *testLabel = [UILabel new];
[testLabel setFont:[UIFont systemFontOfSize:16]];
[testLabel setText:#"Test text for size context"];
[testLabel setTextAlignment:NSTextAlignmentLeft];
[testLabel setNumberOfLines:0]; //0 - infiniy lines if not enough space more
[testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; // Autolayout rule: The higher the priority, the more important hold the text.. by horizontal
[testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//some like above but verticale
[testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];// reverse like Hugging but Compression. then lower priority then text croping.
[testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
[testLabel preferredMaxLayoutWidth:200];//Start new line each 200 points width
[testLabel setPreferredMaxLayoutWidth:YES];// resize Font size by label size.
[testLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; // Disable autolaresize, enable autolayouts
At First read the best answer for any question about UILabel
And also by using following code you can create dynamic size of UILabel
UILabel *myLabelName = [[UILabel alloc] init];
myLabelName.backgroundColor = [UIColor redColor];
[myLabelName setFont: [UIFont fontWithName:#"OpenSans" size:13]]; // set font and it's size as per your requirement.
myLabelName.textAlignment = NSTextAlignmentLeft;
myLabelName.frame = CGRectMake(lblComplainTitle.frame.origin.x + lblComplainTitle.frame.size.width + 7, lblComplainTitle.frame.origin.y +2, 160, 20);
myLabelName.text = #"This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text.";
myLabelName.textColor = [UIColor blackColor];
[self setDynamicHeightOfLabel:myLabelName withLblWidth:160 andFontSize:13]; // here in this function you need to pass object of label with width (as you wabt) and font size
[self.view addSubview:myLabelName];
Code of function, name is setDynamicHeightOfLabel:withLblWidth:andFontSize
-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
CGRect lblFrame = myLabel.frame;
lblFrame.size.height = expecteingmyLabelSize.height;
myLabel.frame = lblFrame;
int addressLine = myLabel.frame.size.height/fontSize;
myLabel.numberOfLines = addressLine;
}
I create function because you can create any label with dynamic size without use of repeated code.
There is a couple of steps that you have to do to achieve this using autolayout.
Set layout constrains for the label.
Set height constraint with low priority.
Set numberOfLines to 0 to allow multiline text.
Set preferredMaxLayoutWidth for the label.
The preferredMaxLayoutWidth is used by label to calculate its height.
This property affects the size of the label when layout constraints
are applied to it. During layout, if the text extends beyond the width
specified by this property, the additional text is flowed to one or
more new lines, thereby increasing the height of the label.
Hi you can manage the width of lable by doing so programatically,
CGRect frame;
frame =self.yourLabel.frame;
frame.size.width +=20;
self.yourLabel.frame=frame;
Thanks

Dynamically adjust font size in UINavigationBar

What I want to do is this :
I've a navigation bar using an image covering the whole bar and looking like this:
Now I want to put the title on the black part of the bar and so it doesn't impinge on the icon.
This title may be very short or very long. So I tried this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
label.backgroundColor = [UIColor blackColor];
label.numberOfLines = 2;
label.minimumFontSize = 8.;
label.adjustsFontSizeToFitWidth = YES;
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text = name;
self.navigationItem.titleView = label;
But this is not working because the label's width grows if needed upon the label and exceeds 200px.
What should I do then?
Thanks for your help.
PS : the camera icon is not a button.
Your code works fine in iOS 7.0
Since you are using UITextAlignmentCenter (instead of NSTextAlignmentCenter), and also using minimumFontSize, I am assuming you are running on an earlier version of iOS, since these were both deprecated in iOS 6.0
The main problem is that adjustsFontSizeToFitWidth only works in iOS6 and earlier if the numberOfLines is set to 1
As you can see in the apple developer documentation
https://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/adjustsFontSizeToFitWidth
If you necessarily want two lines and also want the font size to adjust... you could maybe just use the length property of your name string, which would tell you how many characters are in the string, and you could then set the font size according to the length of the string

Resources