Setting adjustsFontSizeToFitWidth globally for all UILabels
我们有一个多语言应用程序,其中标签中的文本大小可以根据使用的语言而改变。例如在很多空间中,德语比英语使用更多的空间。在这种情况下,iOS 会自动缩小文本,用".."替换中间的字母
例如取消变成 Ca..el
通过在标签中将
有没有一种方法可以为应用程序中的所有
我认为更简洁的方法是为 UILabel 创建一个类别,如下所示:
1 2 3 4 5 6 7 8 | #import <UIKit/UIKit.h> @interface UILabel (FR) @property(nonatomic) UIBaselineAdjustment baselineAdjustment; @property(nonatomic) CGFloat minimumScaleFactor; @end |
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #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 |
并将其包含在您的 Prefix.h 文件中
我认为您可以在 ViewDidLoad 为应用程序的每个 ViewController 添加以下代码:
1 2 3 4 5 6 7 8 | NSArray *allSub = [self.view subviews]; for (id sub in allSub) { if ([sub isKindOfClass:[UILabel class]]) { UILabel *lb =sub; lb.adjustsFontSizeToFitWidth = YES; } } |