|
@@ -0,0 +1,95 @@
|
|
|
+//
|
|
|
+// XCFontScaleCategory.m
|
|
|
+// AFNetworking
|
|
|
+//
|
|
|
+// Created by 邢铖 on 2024/4/5.
|
|
|
+//
|
|
|
+
|
|
|
+#import "XCFontScaleCategory.h"
|
|
|
+#import <XCPublicModule/XCPublicModule.h>
|
|
|
+#import "XCFontScale.h"
|
|
|
+#import <YYText/YYText.h>
|
|
|
+
|
|
|
+@implementation UIView (XCFontScaleCategory)
|
|
|
+
|
|
|
+- (BOOL)enableAutoFontScale {
|
|
|
+ return objc_getAssociatedObject(self, @selector(enableAutoFontScale));
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setEnableAutoFontScale:(BOOL)enableAutoFontScale {
|
|
|
+ objc_setAssociatedObject(self, @selector(enableAutoFontScale), (__bridge id _Nullable)((void *)enableAutoFontScale), OBJC_ASSOCIATION_ASSIGN);
|
|
|
+}
|
|
|
+
|
|
|
+@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);
|
|
|
+ }
|
|
|
+}
|
|
|
+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);
|
|
|
+ }
|
|
|
+}
|
|
|
+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;
|