From 4d44a181e93c0fc26f10586c025aac4958068910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20Kn=C3=B6chel?= Date: Mon, 16 Aug 2021 15:58:38 +0200 Subject: [PATCH] feat(ios): specify additional parameters to SFSymbol system image (#12665) --- apidoc/Titanium/UI/iOS/iOS.yml | 27 ++++++++++++++++--- iphone/Classes/TiUINavBarButton.m | 12 ++++++--- iphone/Classes/TiUIiOSProxy.m | 10 +++++-- .../TitaniumKit/Sources/API/TiBlob.h | 2 +- .../TitaniumKit/Sources/API/TiBlob.m | 18 +++++++++++-- .../TitaniumKit/Sources/API/TiUtils.h | 8 ++++++ .../TitaniumKit/Sources/API/TiUtils.m | 23 ++++++++++++++++ tests/Resources/ti.ui.ios.test.js | 2 ++ 8 files changed, 91 insertions(+), 11 deletions(-) diff --git a/apidoc/Titanium/UI/iOS/iOS.yml b/apidoc/Titanium/UI/iOS/iOS.yml index d0d2e3fa65c..4f696e4d2b5 100644 --- a/apidoc/Titanium/UI/iOS/iOS.yml +++ b/apidoc/Titanium/UI/iOS/iOS.yml @@ -27,13 +27,18 @@ methods: summary: | Get image from SF Symbols provided by Apple. description: | - More details on SF Symbols can be found on https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview. - One can download SF Symbols app from https://developer.apple.com/design/downloads/SF-Symbols.dmg and - use it to lookup the inbuilt SF Symbol name. + More details on SF Symbols can be found [here](https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview). + One can download SF Symbols app from [here](https://developer.apple.com/design/downloads/SF-Symbols.dmg) and + use it to lookup the built-in SF Symbol name. parameters: - name: name summary: Name of SF Symbol. type: String + - name: parameters + optional: true + summary: | + Additional parameters to pass to the SF Symbol. Available in Titanium SDK 10.1.0 and later + type: SystemImageParameters returns: type: Titanium.Blob since: "8.2.0" @@ -1722,3 +1727,19 @@ properties: type: Number since: "8.0.0" permission: read-only + +--- +name: SystemImageParameters +summary: | + Dictionary object of parameters for the 2nd parameter of the method + to pass additional configuration options like `weight` and `size`. Available in Titanium SDK 10.1.0 + and later. +properties: + - name: weight + summary: | + The symbol weight to use for the SF Symbol. + Can be one of `ultralight`, `light`, `thin`, `normal`, `semibold`, `bold`, `heavy` or `black`. + type: String + - name: size + summary: The symbol point size to use for the SF Symbol + type: Number diff --git a/iphone/Classes/TiUINavBarButton.m b/iphone/Classes/TiUINavBarButton.m index 44e3edcd945..9252cb39772 100644 --- a/iphone/Classes/TiUINavBarButton.m +++ b/iphone/Classes/TiUINavBarButton.m @@ -114,9 +114,15 @@ - (id)initWithProxy:(TiUIButtonProxy *)proxy_ [[proxy_ view] setBounds:bounds]; #endif } else if (image != nil) { - NSURL *url = [TiUtils toURL:image proxy:proxy_]; - UIImage *theimage = [[ImageLoader sharedLoader] loadImmediateStretchableImage:url]; - self = [super initWithImage:theimage style:[self style:proxy_] target:self action:@selector(clicked:)]; + UIImage *nativeImage; + // The image can be a raw image (e.g. for blobs / system icons) + if ([image isKindOfClass:[TiBlob class]]) { + nativeImage = [(TiBlob *)image image]; + } else { + NSURL *url = [TiUtils toURL:image proxy:proxy_]; + nativeImage = [[ImageLoader sharedLoader] loadImmediateStretchableImage:url]; + } + self = [super initWithImage:nativeImage style:[self style:proxy_] target:self action:@selector(clicked:)]; } else { self = [super initWithTitle:[self title:proxy_] style:[self style:proxy_] target:self action:@selector(clicked:)]; self.tintColor = [proxy_ valueForKey:@"color"] ? [TiUtils colorValue:[proxy_ valueForKey:@"color"]].color : [TiUtils colorValue:[proxy_ valueForKey:@"tintColor"]].color; diff --git a/iphone/Classes/TiUIiOSProxy.m b/iphone/Classes/TiUIiOSProxy.m index a50112b1225..3a37fb8f06e 100644 --- a/iphone/Classes/TiUIiOSProxy.m +++ b/iphone/Classes/TiUIiOSProxy.m @@ -452,8 +452,14 @@ - (TiBlob *)systemImage:(id)arg if (![TiUtils isIOSVersionOrGreater:@"13.0"]) { return nil; } - ENSURE_SINGLE_ARG_OR_NIL(arg, NSString); - TiBlob *blob = [[[TiBlob alloc] initWithSystemImage:arg] autorelease]; + + NSString *systemImage = nil; + NSDictionary *parameters = nil; + + ENSURE_ARG_OR_NIL_AT_INDEX(systemImage, arg, 0, NSString); + ENSURE_ARG_OR_NIL_AT_INDEX(parameters, arg, 1, NSDictionary); + + TiBlob *blob = [[[TiBlob alloc] initWithSystemImage:systemImage andParameters:parameters] autorelease]; return blob; } diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h index a2bf689ae97..2e0977c6e39 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h @@ -141,7 +141,7 @@ typedef enum { Initialize the blob with a system image. @param imageName The system image name */ -- (id)initWithSystemImage:(NSString *)imageName; +- (id)initWithSystemImage:(NSString *)imageName andParameters:(NSDictionary *)parameters; /** Returns the System Image Name . diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m index 31837665081..587ef2f8daa 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m @@ -149,14 +149,28 @@ - (id)initWithImage:(UIImage *)image_ return self; } -- (id)initWithSystemImage:(NSString *)imageName +- (id)initWithSystemImage:(NSString *)imageName andParameters:(NSDictionary *)parameters { if (![TiUtils isIOSVersionOrGreater:@"13.0"]) { return nil; } if (self = [super init]) { - image = [[UIImage systemImageNamed:imageName] retain]; + if (parameters == nil) { + image = [[UIImage systemImageNamed:imageName] retain]; + } else { + UIImageSymbolWeight nativeWeight = [TiUtils symbolWeightFromString:parameters[@"weight"]]; + CGFloat nativeSize = [TiUtils floatValue:parameters[@"size"] def:0.0]; + UIImageSymbolConfiguration *configuration; + + if (nativeSize > 0) { + configuration = [UIImageSymbolConfiguration configurationWithPointSize:nativeSize weight:nativeWeight scale:UIImageSymbolScaleDefault]; + } else { + configuration = [UIImageSymbolConfiguration configurationWithWeight:nativeWeight]; + } + + image = [[UIImage systemImageNamed:imageName withConfiguration:configuration] retain]; + } type = TiBlobTypeSystemImage; systemImageName = [imageName retain]; mimetype = [([UIImageAlpha hasAlpha:image] ? MIMETYPE_PNG : MIMETYPE_JPEG) copy]; diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.h b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.h index 3fc9cd39eda..5f8c7d29bc6 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.h +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.h @@ -868,4 +868,12 @@ typedef enum { + (BOOL)isMacOS; +/** + Maps a string-based symbol weight (e.g. for SFSymbols) to a native symbol weight. + + @param string The raw string to map. + @return The mapped symbol weight. + */ ++ (UIImageSymbolWeight)symbolWeightFromString:(NSString *)string NS_AVAILABLE_IOS(13_0); + @end diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m index 4b8baf52b51..f6e8eb959ea 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m @@ -2307,4 +2307,27 @@ + (BOOL)isHyperloopAvailable return isHyperloopAvailable; } ++ (UIImageSymbolWeight)symbolWeightFromString:(NSString *)string +{ + if ([string isEqualToString:@"ultralight"]) { + return UIImageSymbolWeightUltraLight; + } else if ([string isEqualToString:@"light"]) { + return UIImageSymbolWeightLight; + } else if ([string isEqualToString:@"thin"]) { + return UIImageSymbolWeightThin; + } else if ([string isEqualToString:@"normal"]) { + return UIImageSymbolWeightRegular; + } else if ([string isEqualToString:@"semibold"]) { + return UIImageSymbolWeightSemibold; + } else if ([string isEqualToString:@"bold"]) { + return UIImageSymbolWeightBold; + } else if ([string isEqualToString:@"heavy"]) { + return UIImageSymbolWeightHeavy; + } else if ([string isEqualToString:@"black"]) { + return UIImageSymbolWeightBlack; + } + + return UIImageSymbolWeightRegular; +} + @end diff --git a/tests/Resources/ti.ui.ios.test.js b/tests/Resources/ti.ui.ios.test.js index 5437d8d80ea..e1557ec70e9 100644 --- a/tests/Resources/ti.ui.ios.test.js +++ b/tests/Resources/ti.ui.ios.test.js @@ -136,6 +136,8 @@ describe.ios('Titanium.UI.iOS', function () { should(Ti.UI.iOS.systemImage).be.a.Function(); const systemImage = Ti.UI.iOS.systemImage('drop.triangle.fill'); should(systemImage).be.an.Object(); + const systemImageWithParameters = Ti.UI.iOS.systemImage('drop.triangle.fill', { weight: 'bold', size: 60 }); + should(systemImageWithParameters).be.an.Object(); } });