Skip to content

Commit

Permalink
feat(ios): specify additional parameters to SFSymbol system image (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn authored Aug 16, 2021
1 parent 1621727 commit 4d44a18
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 11 deletions.
27 changes: 24 additions & 3 deletions apidoc/Titanium/UI/iOS/iOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <Titanium.UI.iOS.systemButton> 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
12 changes: 9 additions & 3 deletions iphone/Classes/TiUINavBarButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions iphone/Classes/TiUIiOSProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSString *, id> *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;
}

Expand Down
2 changes: 1 addition & 1 deletion iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
18 changes: 16 additions & 2 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 8 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions tests/Resources/ti.ui.ios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand Down

0 comments on commit 4d44a18

Please sign in to comment.