Reputation: 1834
We have a multilingual app in which the size of text in labels can change depending on the language in use. e.g. German uses more space than English in a lot of spaces. In such cases, iOS automatically shrinks the text, substituting the middle letters with ".."
e.g. Cancel becomes Ca..el
It's easy to fix these cases individually by setting the adjustsFontSizeToFitWidth
to YES in the label. But it's laborious to do so for every single label out there.
Is there a way one could set it to YES globally for all UILabel
instances in the app? I tried using UIAppearance but it seems that it does not support the adjustsFontSizeToFitWidth
property.
Upvotes: 3
Views: 317
Reputation: 2770
I think more cleaner way is to do a category for UILabel like this:
#import <UIKit/UIKit.h>
@interface UILabel (FR)
@property(nonatomic) UIBaselineAdjustment baselineAdjustment;
@property(nonatomic) CGFloat minimumScaleFactor;
@end
.
#import "UILabel+FR.h"
@implementation UILabel (FR)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
- (void)didMoveToSuperview {
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.5;
[super didMoveToSuperview];
}
#pragma clang diagnostic pop
@end
And include it in your Prefix.h file
Upvotes: 1
Reputation: 84
I think you can add the below code at ViewDidLoad for each ViewController of app:
NSArray *allSub = [self.view subviews];
for (id sub in allSub) {
if ([sub isKindOfClass:[UILabel class]]) {
UILabel *lb =sub;
lb.adjustsFontSizeToFitWidth = YES;
}
}
Upvotes: 1