ソースを参照

【功能】增加字体适配

xcbosa mbp16 1 年間 前
コミット
f27f15d8f6

+ 2 - 0
XCTheme.podspec

@@ -35,6 +35,8 @@ A Theme Manager for C Code Develop & C Notebook.
   s.dependency 'XCModuleLoader'
   s.dependency 'XCZip'
   s.dependency 'XCNotificationCenter'
+  s.dependency 'XCPublicModule'
+  s.dependency 'YYText'
   
   # s.resource_bundles = {
   #   'XCTheme' => ['XCTheme/Assets/*.png']

+ 28 - 0
XCTheme/Classes/Business/FontScale/XCFontScale.h

@@ -0,0 +1,28 @@
+//
+//  XCFontScale.h
+//  AFNetworking
+//
+//  Created by 邢铖 on 2024/4/5.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface XCFontScale : NSObject
+
+@property (nonatomic, strong, readonly, class) XCFontScale *shared;
+
+/// Range [0, 1], the factor of font
+@property (nonatomic, assign) CGFloat factorValue;
+
+- (CGFloat)fontSizeWithFactor:(CGFloat)fontSize;
+- (UIFont *)fontWithFactor:(UIFont *)font;
+
+- (void)registerTarget:(NSObject *)target fontSizeChangeBlock:(nullable void (^)(XCFontScale *))block;
+
+- (void)sendFontSizeChangeNotificationToObservers;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 109 - 0
XCTheme/Classes/Business/FontScale/XCFontScale.m

@@ -0,0 +1,109 @@
+//
+//  XCFontScale.m
+//  AFNetworking
+//
+//  Created by 邢铖 on 2024/4/5.
+//
+
+#import "XCFontScale.h"
+
+@interface XCFontScaleChangeBlock : NSObject
+
+@property (nonatomic, weak) NSObject *weakObject;
+@property (nonatomic, copy) void (^block)(XCFontScale *);
+
+@end
+
+@implementation XCFontScaleChangeBlock
+
+@end
+
+@interface XCFontScale ()
+
+@property (nonatomic, strong) NSMutableArray<XCFontScaleChangeBlock *> *fontSizeObservers;
+@property (nonatomic, strong) NSLock *fontSizeChangeObserverLock;
+@property (nonatomic, strong) NSUserDefaults *storage;
+
+@end
+
+NSString *const kXCFontScaleFactorKey = @"factor";
+
+@implementation XCFontScale
+
++ (XCFontScale *)shared {
+    static dispatch_once_t onceToken;
+    static XCFontScale *scale;
+    dispatch_once(&onceToken, ^{
+        scale = [XCFontScale new];
+    });
+    return scale;
+}
+
+- (instancetype)init {
+    self = [super init];
+    if (self) {
+        self.factorValue = [self.storage doubleForKey:kXCFontScaleFactorKey];
+        self.fontSizeObservers = [NSMutableArray new];
+        self.fontSizeChangeObserverLock = [[NSLock alloc] init];
+    }
+    return self;
+}
+
+- (void)setFactorValue:(CGFloat)factorValue {
+    CGFloat oldValue = _factorValue;
+    _factorValue = MIN(MAX(factorValue, 0.f), 1.f);
+    if (fabs(oldValue - _factorValue) > CGFLOAT_EPSILON) {
+        [self sendFontSizeChangeNotificationToObservers];
+        [self.storage setDouble:self.factorValue forKey:kXCFontScaleFactorKey];
+    }
+}
+
+- (CGFloat)fontSizeWithFactor:(CGFloat)fontSize {
+    CGFloat multipy = 1.f + self.factorValue;
+    return fontSize * multipy;
+}
+
+- (UIFont *)fontWithFactor:(UIFont *)font {
+    return [UIFont fontWithDescriptor:font.fontDescriptor size:[self fontSizeWithFactor:font.pointSize]];
+}
+
+- (void)registerTarget:(NSObject *)target fontSizeChangeBlock:(nullable void (^)(XCFontScale *))block {
+    XCFontScaleChangeBlock *changeBlock = [XCFontScaleChangeBlock new];
+    changeBlock.weakObject = target;
+    changeBlock.block = block;
+    [self.fontSizeChangeObserverLock lock];
+    [self.fontSizeObservers addObject:changeBlock];
+    [self.fontSizeChangeObserverLock unlock];
+}
+
+- (void)sendFontSizeChangeNotificationToObservers {
+    [self.fontSizeChangeObserverLock lock];
+    NSArray *observers = [self.fontSizeObservers copy];
+    NSInteger index = 0;
+    NSMutableIndexSet *indexNeedToRemoval = [NSMutableIndexSet new];
+    NSMutableArray *needCallBlocks = [NSMutableArray new];
+    for (XCFontScaleChangeBlock *block in observers) {
+        NSObject *strongObject = block.weakObject;
+        void (^strongBlock)(XCFontScale *) = block.block;
+        if (strongObject && strongBlock) {
+            [needCallBlocks addObject:strongBlock];
+        } else {
+            [indexNeedToRemoval addIndex:index];
+        }
+        index++;
+    }
+    [self.fontSizeObservers removeObjectsAtIndexes:[indexNeedToRemoval copy]];
+    [self.fontSizeChangeObserverLock unlock];
+    for (void (^block)(XCFontScale *) in needCallBlocks) {
+        block(self);
+    }
+}
+
+- (NSUserDefaults *)storage {
+    if (!_storage) {
+        _storage = [[NSUserDefaults alloc] initWithSuiteName:NSStringFromClass(self.class)];
+    }
+    return _storage;
+}
+
+@end

+ 18 - 0
XCTheme/Classes/Business/FontScale/XCFontScaleCategory.h

@@ -0,0 +1,18 @@
+//
+//  XCFontScaleCategory.h
+//  AFNetworking
+//
+//  Created by 邢铖 on 2024/4/5.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface UIView (XCFontScaleCategory)
+
+@property (nonatomic, assign) BOOL enableAutoFontScale;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 95 - 0
XCTheme/Classes/Business/FontScale/XCFontScaleCategory.m

@@ -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;

+ 0 - 0
XCTheme/Classes/Business/XCThemeManager.swift → XCTheme/Classes/Business/Theme/XCThemeManager.swift


+ 0 - 0
XCTheme/Classes/Business/XCThemeService.m → XCTheme/Classes/Business/Theme/XCThemeService.m


+ 0 - 0
XCTheme/Classes/Business/XCThemeSpec.swift → XCTheme/Classes/Business/Theme/XCThemeSpec.swift


+ 0 - 0
XCTheme/Classes/Business/XCThemeSpecModel.h → XCTheme/Classes/Business/Theme/XCThemeSpecModel.h


+ 0 - 0
XCTheme/Classes/Business/XCThemeSpecModel.m → XCTheme/Classes/Business/Theme/XCThemeSpecModel.m