3

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.

er0
  • 1,746
  • 2
  • 16
  • 31

2 Answers2

1

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;
    }
}
  • Nice. But I expect that you'd have to do a recursive descent, in case there are UILabels within other subviews. – er0 Jun 05 '14 at 08:28
  • Yes. Thanks because your idea. We can refer this [link](http://stackoverflow.com/questions/7243888/how-to-list-out-all-the-subviews-in-a-uiviewcontroller-in-ios) – hoptqVN.dev Jun 05 '14 at 09:54
1

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

Cherpak Evgeny
  • 2,659
  • 22
  • 29