|
@@ -8,88 +8,120 @@
|
|
|
#import "XCFontScaleCategory.h"
|
|
|
#import <XCPublicModule/XCPublicModule.h>
|
|
|
#import "XCFontScale.h"
|
|
|
-#import <YYText/YYText.h>
|
|
|
|
|
|
-@implementation UIView (XCFontScaleCategory)
|
|
|
+#define InvokeFontSizeChangeBlock void (^fontSizeChangeBlock)(void) = self.fontSizeChangedBlock; \
|
|
|
+ if (fontSizeChangeBlock) { \
|
|
|
+ fontSizeChangeBlock(); \
|
|
|
+ }
|
|
|
+
|
|
|
+@implementation UILabel (XCFontScaleCategory)
|
|
|
+
|
|
|
+- (UIFont *)autoScaleFont {
|
|
|
+ return objc_getAssociatedObject(self, @selector(autoScaleFont));
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setAutoScaleFont:(UIFont *)autoScaleFont {
|
|
|
+ objc_setAssociatedObject(self, @selector(autoScaleFont), autoScaleFont, OBJC_ASSOCIATION_RETAIN);
|
|
|
+ self.font = [XCFontScale.shared fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ XC_WEAKIFY_SELF;
|
|
|
+ [XCFontScale.shared replaceTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
+ XC_STRONG_SELF;
|
|
|
+ self.font = [fontScale fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ }];
|
|
|
+}
|
|
|
|
|
|
-- (BOOL)enableAutoFontScale {
|
|
|
- return objc_getAssociatedObject(self, @selector(enableAutoFontScale));
|
|
|
+- (void (^)(void))fontSizeChangedBlock {
|
|
|
+ return objc_getAssociatedObject(self, @selector(fontSizeChangedBlock));
|
|
|
}
|
|
|
|
|
|
-- (void)setEnableAutoFontScale:(BOOL)enableAutoFontScale {
|
|
|
- objc_setAssociatedObject(self, @selector(enableAutoFontScale), (__bridge id _Nullable)((void *)enableAutoFontScale), OBJC_ASSOCIATION_ASSIGN);
|
|
|
+- (void)setFontSizeChangedBlock:(void (^)(void))fontSizeChangedBlock {
|
|
|
+ objc_setAssociatedObject(self, @selector(fontSizeChangedBlock), fontSizeChangedBlock, OBJC_ASSOCIATION_COPY);
|
|
|
}
|
|
|
|
|
|
@end
|
|
|
|
|
|
-begin_override(HookUILabelFont, UILabel, setFont:, void, setFont:(UIFont *)font) {
|
|
|
- SEL selector = @selector(swizzled_setFont:);
|
|
|
- IMP imp = class_getMethodImplementation([UILabel class], selector);
|
|
|
- void (*method)(id, SEL, UIFont *) = (void *)imp;
|
|
|
- if (self.enableAutoFontScale) {
|
|
|
- UIFont *capturedFont = [font copy];
|
|
|
- method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
- XC_WEAKIFY_SELF;
|
|
|
- [XCFontScale.shared registerTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
- XC_STRONG_SELF;
|
|
|
- method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
- }];
|
|
|
- } else {
|
|
|
- method(self, selector, font);
|
|
|
- }
|
|
|
+@implementation UITextView (XCFontScaleCategory)
|
|
|
+
|
|
|
+- (UIFont *)autoScaleFont {
|
|
|
+ return objc_getAssociatedObject(self, @selector(autoScaleFont));
|
|
|
}
|
|
|
-end_override;
|
|
|
-
|
|
|
-begin_override(HookYYLabelFont, YYLabel, setFont:, void, setFont:(UIFont *)font) {
|
|
|
- SEL selector = @selector(swizzled_setFont:);
|
|
|
- IMP imp = class_getMethodImplementation([UILabel class], selector);
|
|
|
- void (*method)(id, SEL, UIFont *) = (void *)imp;
|
|
|
- if (self.enableAutoFontScale) {
|
|
|
- UIFont *capturedFont = [font copy];
|
|
|
- method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
- XC_WEAKIFY_SELF;
|
|
|
- [XCFontScale.shared registerTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
- XC_STRONG_SELF;
|
|
|
- method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
- }];
|
|
|
- } else {
|
|
|
- method(self, selector, font);
|
|
|
- }
|
|
|
+
|
|
|
+- (void)setAutoScaleFont:(UIFont *)autoScaleFont {
|
|
|
+ objc_setAssociatedObject(self, @selector(autoScaleFont), autoScaleFont, OBJC_ASSOCIATION_RETAIN);
|
|
|
+ self.font = [XCFontScale.shared fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ XC_WEAKIFY_SELF;
|
|
|
+ [XCFontScale.shared replaceTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
+ XC_STRONG_SELF;
|
|
|
+ self.font = [fontScale fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void (^)(void))fontSizeChangedBlock {
|
|
|
+ return objc_getAssociatedObject(self, @selector(fontSizeChangedBlock));
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setFontSizeChangedBlock:(void (^)(void))fontSizeChangedBlock {
|
|
|
+ objc_setAssociatedObject(self, @selector(fontSizeChangedBlock), fontSizeChangedBlock, OBJC_ASSOCIATION_COPY);
|
|
|
+}
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation YYLabel (XCFontScaleCategory)
|
|
|
+
|
|
|
+- (UIFont *)autoScaleFont {
|
|
|
+ return objc_getAssociatedObject(self, @selector(autoScaleFont));
|
|
|
}
|
|
|
-end_override;
|
|
|
-
|
|
|
-//begin_override(HookUITextView, UITextView, setFont:, void, setFont:(UIFont *)font) {
|
|
|
-// SEL selector = @selector(swizzled_setFont:);
|
|
|
-// IMP imp = class_getMethodImplementation([UILabel class], selector);
|
|
|
-// void (*method)(id, SEL, UIFont *) = (void *)imp;
|
|
|
-// if (self.enableAutoFontScale) {
|
|
|
-// UIFont *capturedFont = [font copy];
|
|
|
-// method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
-// XC_WEAKIFY_SELF;
|
|
|
-// [XCFontScale.shared registerTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
-// XC_STRONG_SELF;
|
|
|
-// method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
-// }];
|
|
|
-// } else {
|
|
|
-// method(self, selector, font);
|
|
|
-// }
|
|
|
-//}
|
|
|
-//end_override;
|
|
|
-
|
|
|
-//begin_override(HookYYTextView, YYTextView, setFont:, void, setFont:(UIFont *)font) {
|
|
|
-// SEL selector = @selector(swizzled_setFont:);
|
|
|
-// IMP imp = class_getMethodImplementation([UILabel class], selector);
|
|
|
-// void (*method)(id, SEL, UIFont *) = (void *)imp;
|
|
|
-// if (self.enableAutoFontScale) {
|
|
|
-// UIFont *capturedFont = [font copy];
|
|
|
-// method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
-// XC_WEAKIFY_SELF;
|
|
|
-// [XCFontScale.shared registerTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
-// XC_STRONG_SELF;
|
|
|
-// method(self, selector, [XCFontScale.shared fontWithFactor:capturedFont]);
|
|
|
-// }];
|
|
|
-// } else {
|
|
|
-// method(self, selector, font);
|
|
|
-// }
|
|
|
-//}
|
|
|
-//end_override;
|
|
|
+
|
|
|
+- (void)setAutoScaleFont:(UIFont *)autoScaleFont {
|
|
|
+ objc_setAssociatedObject(self, @selector(autoScaleFont), autoScaleFont, OBJC_ASSOCIATION_RETAIN);
|
|
|
+ self.font = [XCFontScale.shared fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ XC_WEAKIFY_SELF;
|
|
|
+ [XCFontScale.shared replaceTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
+ XC_STRONG_SELF;
|
|
|
+ self.font = [fontScale fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void (^)(void))fontSizeChangedBlock {
|
|
|
+ return objc_getAssociatedObject(self, @selector(fontSizeChangedBlock));
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setFontSizeChangedBlock:(void (^)(void))fontSizeChangedBlock {
|
|
|
+ objc_setAssociatedObject(self, @selector(fontSizeChangedBlock), fontSizeChangedBlock, OBJC_ASSOCIATION_COPY);
|
|
|
+}
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation YYTextView (XCFontScaleCategory)
|
|
|
+
|
|
|
+- (UIFont *)autoScaleFont {
|
|
|
+ return objc_getAssociatedObject(self, @selector(autoScaleFont));
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setAutoScaleFont:(UIFont *)autoScaleFont {
|
|
|
+ objc_setAssociatedObject(self, @selector(autoScaleFont), autoScaleFont, OBJC_ASSOCIATION_RETAIN);
|
|
|
+ self.font = [XCFontScale.shared fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ XC_WEAKIFY_SELF;
|
|
|
+ [XCFontScale.shared replaceTarget:self fontSizeChangeBlock:^(XCFontScale * _Nonnull fontScale) {
|
|
|
+ XC_STRONG_SELF;
|
|
|
+ self.font = [fontScale fontWithFactor:autoScaleFont];
|
|
|
+ InvokeFontSizeChangeBlock;
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void (^)(void))fontSizeChangedBlock {
|
|
|
+ return objc_getAssociatedObject(self, @selector(fontSizeChangedBlock));
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setFontSizeChangedBlock:(void (^)(void))fontSizeChangedBlock {
|
|
|
+ objc_setAssociatedObject(self, @selector(fontSizeChangedBlock), fontSizeChangedBlock, OBJC_ASSOCIATION_COPY);
|
|
|
+}
|
|
|
+
|
|
|
+@end
|