.h文件
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIFont (FontAdaptive)
@end
@interface UIButton (FontAdaptive)
@end
@interface UILabel (FontAdaptive)
@end
@interface UITextField (FontAdaptive)
@end
@interface UITextView (FontAdaptive)
@end
NS_ASSUME_NONNULL_END
.m 文件
#define Adaptive_coefficient [[UIScreen mainScreen]bounds].size.width/375.0f
@implementation UIFont (FontAdaptive)
+ (void)load {
Method selfMethod = class_getClassMethod([self class], @selector(runtimeFitFont:));
Method systenMethod = class_getClassMethod([self class], @selector(systemFontOfSize:));
method_exchangeImplementations(selfMethod, systenMethod);
}
+ (UIFont *)runtimeFitFont:(CGFloat)fontSize {
UIFont *fitFont = nil;
//这里并不会造成递归调用,方法已经被交换
fitFont = [UIFont runtimeFitFont:fontSize * Adaptive_coefficient];
return fitFont;
}
@end
@implementation UIButton (FontAdaptive)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
method_exchangeImplementations(imp, runtimeImp);
}
- (id)runtimeInitWithCoder:(NSCoder*)aDecode{
[self runtimeInitWithCoder:aDecode];
if (self) {
//部分不像改变字体的 把tag值设置成2222跳过
if(self.titleLabel.tag != 2222){
CGFloat fontSize = self.titleLabel.font.pointSize;
self.titleLabel.font = [UIFont systemFontOfSize:fontSize];
}
}
return self;
}
@end
@implementation UILabel (FontAdaptive)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
method_exchangeImplementations(imp, runtimeImp);
}
- (id)runtimeInitWithCoder:(NSCoder*)aDecode{
[self runtimeInitWithCoder:aDecode];
if (self) {
//部分不像改变字体的 把tag值设置成2222跳过
if(self.tag != 2222){
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize];
}
}
return self;
}
@end
@implementation UITextField (FontAdaptive)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
method_exchangeImplementations(imp, runtimeImp);
}
- (id)runtimeInitWithCoder:(NSCoder*)aDecode{
[self runtimeInitWithCoder:aDecode];
if (self) {
//部分不像改变字体的 把tag值设置成2222跳过
if(self.tag != 2222){
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize];
}
}
return self;
}
@end
@implementation UITextView (FontAdaptive)
+ (void)load{
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method runtimeImp = class_getInstanceMethod([self class], @selector(runtimeInitWithCoder:));
method_exchangeImplementations(imp, runtimeImp);
}
- (id)runtimeInitWithCoder:(NSCoder*)aDecode{
[self runtimeInitWithCoder:aDecode];
if (self) {
//部分不像改变字体的 把tag值设置成2222跳过
if(self.tag != 2222){
CGFloat fontSize = self.font.pointSize;
self.font = [UIFont systemFontOfSize:fontSize];
}
}
return self;
}
@end
网友评论