Last active
August 28, 2018 13:20
-
-
Save Jpoliachik/ed5cb38b480bc544427ce0db529786bf to your computer and use it in GitHub Desktop.
React Native iOS Accessibility Font Scaling Workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RCTAccessibilityManager+FontMultipliers.m | |
// | |
// This category is a workaround React Native's default Font Sizing Accessibility settings | |
// | |
// Swizzle the getter of the font size multipliers so we can limit the max size. | |
// | |
#import "RCTAccessibilityManager+FontMultipliers.h" | |
#import <objc/runtime.h> | |
@implementation RCTAccessibilityManager (FontMultipliers) | |
+ (void)load { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
Class class = [self class]; | |
SEL originalSelector = @selector(multipliers); | |
SEL swizzledSelector = @selector(custom_accessibility_font_multipliers); | |
Method originalMethod = class_getInstanceMethod(class, originalSelector); | |
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | |
// When swizzling a class method, use the following: | |
// Class class = object_getClass((id)self); | |
// ... | |
// Method originalMethod = class_getClassMethod(class, originalSelector); | |
// Method swizzledMethod = class_getClassMethod(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); | |
} | |
}); | |
} | |
#pragma mark - Method Swizzling | |
- (NSDictionary<NSString *, NSNumber *> *)custom_accessibility_font_multipliers { | |
return @{UIContentSizeCategoryExtraSmall: @0.823, | |
UIContentSizeCategorySmall: @0.882, | |
UIContentSizeCategoryMedium: @0.941, | |
UIContentSizeCategoryLarge: @1.0, | |
UIContentSizeCategoryExtraLarge: @1.118, | |
UIContentSizeCategoryExtraExtraLarge: @1.235, | |
UIContentSizeCategoryExtraExtraExtraLarge: @1.353, | |
UIContentSizeCategoryAccessibilityMedium: @1.5, // cap at 1.5x | |
UIContentSizeCategoryAccessibilityLarge: @1.5, | |
UIContentSizeCategoryAccessibilityExtraLarge: @1.5, | |
UIContentSizeCategoryAccessibilityExtraExtraLarge: @1.5, | |
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @1.5}; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You missed the Header file: