Dynamically changing font size of UILabel
Single line:
factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8;
factLabel.adjustsFontSizeToFitWidth = YES;
The above code will adjust your text's font size down to (for example) 8
trying to fit your text within the label. numberOfLines = 1
is mandatory.
Multiple lines:
For numberOfLines > 1
there is a method to figure out the size of final text through NSString's sizeWithFont:... UIKit addition methods, for example:
CGSize lLabelSize = [yourText sizeWithFont:factLabel.font
forWidth:factLabel.frame.size.width
lineBreakMode:factLabel.lineBreakMode];
After that you can just resize your label using resulting lLabelSize
, for example (assuming that you will change only label's height):
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSize.height);
iOS6
Single line:
Starting with iOS6, minimumFontSize
has been deprecated. The line
factLabel.minimumFontSize = 8.;
can be changed to:
factLabel.minimumScaleFactor = 8./factLabel.font.pointSize;
iOS7
Multiple lines:
Starting with iOS7, sizeWithFont
becomes deprecated.
Multiline case is reduced to:
factLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width, CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, expectSize.width, expectSize.height);
iOS 13 (Swift 5):
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
UILabel dynamically change font size and keep position?
Ended doing this :
let center:CGPoint=editedView.center // first take the center
label.font = label.font.withSize(max*percentage)
label.sizeToFit() // set right size
label.center=center //then set it back
change font size in UILabel based on auto layout (swift)
UILabel
has no methods automatically reducing font to fit content size or height - only width.
So you should do it by manually changing font due to size changes (don't forget to test result sizes with UILabel
not NSString.sizeWithFont:
: results would be slightly different).
You can try to set constraints of your container to preserve UILabel
's aspect ratio. This way when you reduce height, width will be reduced too and UILabel.adjustsFontSizeToFitWidth
would do it's work. Should be rough, but enough in you case and much easier and faster to implement.
UPDATE
Forgot to mention. I spoke about your concrete example. Behaviour depends on numberOfLines
and lineBreakMode
. Making the first != 1 and second something like .byTruncatingTail
(default value) will display close to necessary (but, for example, will wrap words when width isn't enough - while you expect no wrapping at all).
UILabel Dynamic Font size keep breaking
There is minimum font shrink property is available. it shrinks the font size as minimum ratio with the current point size. If the content become more then the ratio of fonts then it will defiantly shows the truncation dots(...) at the end.
For example :
Here I am setting up the property with minimum font scale is 0.4.
And here you can see different texts applied with the same property.
In first and second label will adjust the font size because content is less and it adjust as per minimum font scale ratio. but in 3rd the minimum font ratio and the content of the label is does not matched then it will show the dots(...) with truncation.
For this issue you should know that how mach content should be shown on that label or for batter use you can use multiline label.
Hope you will get help from the answer.
How to dynamically increase and decrease UILabel's text smoothly as font size varies ios
Instead of changing the font size, you can just use UIView's transform, e.g
self.labelAttendCount.transform = CGAffineTransformMakeScale(1.5, 1.5);
How to make UILabel and UITextField font size to be relative to screen size
You can manually change UIlabel
or UITextfield
font in viewDidLayoutSubviews
method of your ViewController
or layoutSubviews
method of your superView
. Like this:
CGRect screenSize = [UIScreen mainScreen].bounds;
textField.font = [UIFont fontWithName:@"YourFont-Name"
size:screenSize.size.width/15.f];
Change the height of UILabel dynamically based on content
Using below you can get the height of the label
- text - text of the label
- font - font used in label
width - width of the label
-(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{
CGSize constraint = CGSizeMake(width , 20000.0f);
CGSize title_size;
float totalHeight;
SEL selector = @selector(boundingRectWithSize:options:attributes:context:);
if ([text respondsToSelector:selector]) {
title_size = [text boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName : font }
context:nil].size;
totalHeight = ceil(title_size.height);
} else {
title_size = [text sizeWithFont:font
constrainedToSize:constraint
lineBreakMode:NSLineBreakByWordWrapping];
totalHeight = title_size.height ;
}
CGFloat height = MAX(totalHeight, 40.0f);
return height;
}
and create a frame using the height
CGRect frame = questionTitleLbl.frame;
float height = [self getHeightForText:questionTitleLbl.text
withFont:questionTitleLbl.font
andWidth:questionTitleLbl.frame.size.width];
float gap = 2;
cell.questionTitleLbl.frame = CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
height);
Related Topics
Stop Uiwebview from "Bouncing" Vertically
Present Modal View Controller in Half Size Parent Controller
iOS 7 App Icons, Launch Images and Naming Convention While Keeping iOS 6 Icons
Method Overloading in Objective-C
In iOS, How to Store a Secret "Key" That Will Allow Me to Communicate with My Server
Autolayout with Hidden Uiviews
Dynamic Height Issue for Uitableview Cells (Swift)
Objective-C Declare Vars with ({ ... })
iOS Uinavigationbar Button Remains Faded After Segue Back
How to Disable Back Swipe Gesture in Uinavigationcontroller on iOS 7
Detect First Launch of iOS App
Exhaustive Condition of Switch Case in Swift
Uilabel Layer Cornerradius Negatively Impacting Performance
Linker Command Failed with Exit Code 1 (Use -V to See Invocation), Xcode 8, Swift 3
How to Make a Background Image Scale to Screen Size in Swift