From 54fb8a346d3c7ccfff20b27116dca6d4ee3d1066 Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Thu, 28 Sep 2023 13:49:34 +0700 Subject: [PATCH] Workaround[dynamic position]: replace dp: and px: calls with direct find-and-replace Certain iOS versions don't like when expression tries to call either dp: or px: --- Natives/customcontrols/ControlButton.m | 16 ++++++++++++++++ .../NSPredicateUtilitiesExternal.m | 2 ++ 2 files changed, 18 insertions(+) diff --git a/Natives/customcontrols/ControlButton.m b/Natives/customcontrols/ControlButton.m index 40735120..1703898c 100644 --- a/Natives/customcontrols/ControlButton.m +++ b/Natives/customcontrols/ControlButton.m @@ -96,6 +96,21 @@ } } +- (NSString *)processFunctions:(NSString *)string { + NSString *tmpStr; + CGFloat screenScale = UIScreen.mainScreen.scale; + + // FIXME: on certain iOS versions, we cannot invoke dp: and px: in _NSPredicateUtilities + // we gotta do direct replaces here + + // float dp(float px) => px / screenScale + tmpStr = [string stringByReplacingOccurrencesOfString:@"dp(" withString:[NSString stringWithFormat:@"(1.0 / %f * ", screenScale]]; + + // float px(float dp) => screenScale * dp + tmpStr = [tmpStr stringByReplacingOccurrencesOfString:@"px(" withString:[NSString stringWithFormat:@"(%f * ", screenScale]]; + return tmpStr; +} + - (CGFloat)calculateDynamicPos:(NSString *)string { CGRect screenBounds = self.superview.bounds; NSAssert(self.superview, @"Why is it null"); @@ -119,6 +134,7 @@ INSERT_VALUE("margin", ([NSString stringWithFormat:@"%f", 2.0 * screenScale])); INSERT_VALUE("preferred_scale", ([NSString stringWithFormat:@"%f", getPrefFloat(@"control.button_scale")])); + string = [self processFunctions:string]; // NSLog(@"After insert: %@", string); // Calculate, since the dynamic position contains some math equations diff --git a/Natives/customcontrols/NSPredicateUtilitiesExternal.m b/Natives/customcontrols/NSPredicateUtilitiesExternal.m index 3c7a7edd..7b7f0b13 100644 --- a/Natives/customcontrols/NSPredicateUtilitiesExternal.m +++ b/Natives/customcontrols/NSPredicateUtilitiesExternal.m @@ -2,12 +2,14 @@ @implementation NSPredicateUtilitiesExternal +#if 0 // FIXME: doesn't work on certain iOS versions + (NSNumber *)dp:(NSNumber *)number { return @(number.doubleValue / UIScreen.mainScreen.scale); } + (NSNumber *)px:(NSNumber *)number { return @(number.doubleValue * UIScreen.mainScreen.scale); } +#endif + (NSNumber *)cbrt:(NSNumber *)number { return @(cbrt(number.doubleValue));