When I add text to a label with the adjustsFontSizeToFitWidth
set to YES
the text is no longer centred vertically and eventually clips the text at the bottom of the label frame. For a large amount of text it will eventually disappear off the bottom of the label.
This is what happens if you add less text:
This is clipped as I would expect it (i.e. the font size did not reduce, the text was vertically centred in the label and clipped on the top and bottom.
Here is the code to reproduce:
- (void)loadView { [super loadView]; self.view.backgroundColor = [UIColor blueColor]; testLabel = [[UILabel alloc] init]; testLabel.font = [UIFont boldSystemFontOfSize:172]; testLabel.textColor = [UIColor blackColor]; testLabel.adjustsFontSizeToFitWidth = YES; testLabel.numberOfLines = 1; testLabel.frame = CGRectMake(50, 50, 300, 100); testLabel.text = @"123"; [self.view addSubview:testLabel]; }
Should this happen? And how do I get my label to centre vertically irrespective of the number of characters in my label.
Add
testLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
to your code to vertical-center the text on font scale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With