在 Objective-C 中默认启用自动收缩
Enable auto shrink by default in Objective-C
是否有可能为 UILabel
s 和 UIButton
s 启用默认自动收缩?
基本上,我需要它们是 运行 这个代码:
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.5;
即使它们是从 .xib
文件或代码初始化的,也无需替换为自定义 MyLabel
。
尽管我强烈反对(如中所述),但这里有一种使用method swizzling
和category
:
UILabel+SwizzledInitializer.h:
//
// UILabel+SwizzledInitializer.h
//
#import <UIKit/UIKit.h>
@interface UILabel (SwizzledInitializer)
@end
UILabel+SwizzledInitializer.m:
//
// UILabel+SwizzledInitializer.m
//
#import "UILabel+SwizzledInitializer.h"
#import <objc/runtime.h>
@implementation UILabel (SwizzledInitializer)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(initWithFrame:);
SEL swizzledSelector = @selector(initWithFrame_swizzledForAutoShrink:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (instancetype)initWithFrame_swizzledForAutoShrink:(CGRect)frame;
{
self = [self initWithFrame_swizzledForAutoShrink:frame];
if (self) {
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.5;
}
return self;
}
@end
是否有可能为 UILabel
s 和 UIButton
s 启用默认自动收缩?
基本上,我需要它们是 运行 这个代码:
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.5;
即使它们是从 .xib
文件或代码初始化的,也无需替换为自定义 MyLabel
。
尽管我强烈反对(如method swizzling
和category
:
UILabel+SwizzledInitializer.h:
//
// UILabel+SwizzledInitializer.h
//
#import <UIKit/UIKit.h>
@interface UILabel (SwizzledInitializer)
@end
UILabel+SwizzledInitializer.m:
//
// UILabel+SwizzledInitializer.m
//
#import "UILabel+SwizzledInitializer.h"
#import <objc/runtime.h>
@implementation UILabel (SwizzledInitializer)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(initWithFrame:);
SEL swizzledSelector = @selector(initWithFrame_swizzledForAutoShrink:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (instancetype)initWithFrame_swizzledForAutoShrink:(CGRect)frame;
{
self = [self initWithFrame_swizzledForAutoShrink:frame];
if (self) {
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.5;
}
return self;
}
@end