diff options
author | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2019-03-22 13:45:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-22 13:45:39 -0700 |
commit | 1d7b243a06c6c3d2def4d1989e93e90250e87cad (patch) | |
tree | 05394b1f3aaa53b13dfa73bfd5b2ebd918933f98 /platform/darwin/src | |
parent | c78b60bf356ad36fc36bce24a184e046cc8066bb (diff) | |
download | qtlocation-mapboxgl-1d7b243a06c6c3d2def4d1989e93e90250e87cad.tar.gz |
[ios, macos] Support expressions in formatting parameters. (#14198)
Modified MGLAttributedExpression object to support NSExpressions as formatting attributes only. This aligns with GL and Android implementation and specification.
Modified the documentation accordingly, and changed MGLFontSizeAttribute to MGLFontScaleAttribute to clarify the true function of the formatting parameter.
Diffstat (limited to 'platform/darwin/src')
-rw-r--r-- | platform/darwin/src/MGLAttributedExpression.h | 40 | ||||
-rw-r--r-- | platform/darwin/src/MGLAttributedExpression.m | 14 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 27 |
3 files changed, 37 insertions, 44 deletions
diff --git a/platform/darwin/src/MGLAttributedExpression.h b/platform/darwin/src/MGLAttributedExpression.h index 32f1a96dae..ea298c7a44 100644 --- a/platform/darwin/src/MGLAttributedExpression.h +++ b/platform/darwin/src/MGLAttributedExpression.h @@ -2,10 +2,16 @@ NS_ASSUME_NONNULL_BEGIN -typedef NSString * MGLAttributedExpressionKey NS_EXTENSIBLE_STRING_ENUM; +/** Options for `MGLAttributedExpression.attributes`. */ +typedef NSString * MGLAttributedExpressionKey NS_TYPED_ENUM; +/** The font name string array expression used to format the text. */ FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontNamesAttribute; -FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontSizeAttribute; + +/** The font scale number expression relative to `MGLSymbolStyleLayer.textFontSize` used to format the text. */ +FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontScaleAttribute; + +/** The font color expression used to format the text. */ FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontColorAttribute; /** @@ -16,10 +22,10 @@ FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontColorAttrib ```swift let redColor = UIColor.red let expression = NSExpression(forConstantValue: "Foo") - let attributes: Dictionary<MGLAttributedExpressionKey, Any> = [.fontNamesAttribute : ["DIN Offc Pro Italic", - "Arial Unicode MS Regular"], - .fontSizeAttribute: 1.2, - .fontColorAttribute: redColor] + let attributes: [MGLAttributedExpressionKey: NSExpression] = [.fontNamesAttribute : NSExpression(forConstantValue: ["DIN Offc Pro Italic", + "Arial Unicode MS Regular"]), + .fontScaleAttribute: NSExpression(forConstantValue: 1.2), + .fontColorAttribute: NSExpression(forConstantValue: redColor)] let attributedExpression = MGLAttributedExpression(expression, attributes:attributes) ``` @@ -37,22 +43,22 @@ MGL_EXPORT The formatting attributes dictionary. Key | Value Type --- | --- - `MGLFontNamesAttribute` | `NSArray<NSString *>*` - `MGLFontSizeAttribute` | `NSNumber` - `MGLFontColorAttribute` | `UIColor` + `MGLFontNamesAttribute` | An `NSExpression` evaluating to an `NSString` array. + `MGLFontScaleAttribute` | An `NSExpression` evaluating to an `NSNumber` value. + `MGLFontColorAttribute` | An `NSExpression` evaluating to an `UIColor`. */ -@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, id> *attributes; +@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, NSExpression *> *attributes; #else /** The formatting attributes dictionary. Key | Value Type --- | --- - `MGLFontNamesAttribute` | `NSArray<NSString *>*` - `MGLFontSizeAttribute` | `NSNumber` - `MGLFontColorAttribute` | `NSColor` + `MGLFontNamesAttribute` | An `NSExpression` evaluating to an `NSString` array. + `MGLFontScaleAttribute` | An `NSExpression` evaluating to an `NSNumber` value. + `MGLFontColorAttribute` | An `NSExpression` evaluating to an `NSColor` on macos. */ -@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, id> *attributes; +@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, NSExpression *> *attributes; #endif @@ -64,17 +70,17 @@ MGL_EXPORT /** Returns an `MGLAttributedExpression` object initialized with an expression and text format attributes. */ -- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, id> *)attrs; +- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, NSExpression *> *)attrs; /** Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes for font names and font size. */ -+ (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString*> *)fontNames fontSize:(nullable NSNumber *)fontSize; ++ (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString*> *)fontNames fontScale:(nullable NSNumber *)fontScale; /** Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes dictionary. */ -+ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, id> *)attrs; ++ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, NSExpression *> *)attrs; @end diff --git a/platform/darwin/src/MGLAttributedExpression.m b/platform/darwin/src/MGLAttributedExpression.m index 000701f7cf..a34480a957 100644 --- a/platform/darwin/src/MGLAttributedExpression.m +++ b/platform/darwin/src/MGLAttributedExpression.m @@ -2,7 +2,7 @@ #import "MGLLoggingConfiguration_Private.h" const MGLAttributedExpressionKey MGLFontNamesAttribute = @"text-font"; -const MGLAttributedExpressionKey MGLFontSizeAttribute = @"font-scale"; +const MGLAttributedExpressionKey MGLFontScaleAttribute = @"font-scale"; const MGLAttributedExpressionKey MGLFontColorAttribute = @"text-color"; @implementation MGLAttributedExpression @@ -12,24 +12,24 @@ const MGLAttributedExpressionKey MGLFontColorAttribute = @"text-color"; return self; } -+ (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString *> *)fontNames fontSize:(nullable NSNumber *)fontSize { ++ (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString *> *)fontNames fontScale:(nullable NSNumber *)fontScale { MGLAttributedExpression *attributedExpression; NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; if (fontNames && fontNames.count > 0) { - attrs[MGLFontNamesAttribute] = fontNames; + attrs[MGLFontNamesAttribute] = [NSExpression expressionForConstantValue:fontNames]; } - if (fontSize) { - attrs[MGLFontSizeAttribute] = fontSize; + if (fontScale) { + attrs[MGLFontScaleAttribute] = [NSExpression expressionForConstantValue:fontScale]; } attributedExpression = [[self alloc] initWithExpression:expression attributes:attrs]; return attributedExpression; } -+ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey,id> *)attrs { ++ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey, NSExpression *> *)attrs { MGLAttributedExpression *attributedExpression; attributedExpression = [[self alloc] initWithExpression:expression attributes:attrs]; @@ -37,7 +37,7 @@ const MGLAttributedExpressionKey MGLFontColorAttribute = @"text-color"; return attributedExpression; } -- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey,id> *)attrs { +- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey, NSExpression *> *)attrs { if (self = [super init]) { MGLLogInfo(@"Starting %@ initialization.", NSStringFromClass([self class])); diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index b509c12172..4b1fdb818e 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -882,19 +882,8 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { attrs = [NSMutableDictionary dictionaryWithDictionary:argumentObjects[index + 1]]; } - if (attrs.count) { - if (NSArray *fontNames = MGL_OBJC_DYNAMIC_CAST(attrs[MGLFontNamesAttribute], NSArray)) { - attrs[MGLFontNamesAttribute] = fontNames[1]; - } - if (NSArray *colorArray = MGL_OBJC_DYNAMIC_CAST(attrs[MGLFontColorAttribute], NSArray)) { - if ([colorArray[0] isEqualToString:@"rgb"] || [colorArray[0] isEqualToString:@"rgba"]) { - NSArray *colorArguments = [colorArray subarrayWithRange:NSMakeRange(1, colorArray.count - 1)]; - NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(colorArguments); - MGLColor *color = [NSExpression mgl_colorWithRGBComponents:subexpressions]; - - attrs[MGLFontColorAttribute] = color; - } - } + for (NSString *key in attrs.allKeys) { + attrs[key] = [NSExpression expressionWithMGLJSONObject:attrs[key]]; } MGLAttributedExpression *attributedExpression = [[MGLAttributedExpression alloc] initWithExpression:expression attributes:attrs]; @@ -1016,17 +1005,15 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { if ([constantValue isKindOfClass:[MGLAttributedExpression class]]) { MGLAttributedExpression *attributedExpression = (MGLAttributedExpression *)constantValue; id jsonObject = attributedExpression.expression.mgl_jsonExpressionObject; - NSMutableDictionary *attributedDictionary = [NSMutableDictionary dictionary]; + NSMutableDictionary<MGLAttributedExpressionKey, NSExpression *> *attributedDictionary = [NSMutableDictionary dictionary]; if (attributedExpression.attributes) { attributedDictionary = [NSMutableDictionary dictionaryWithDictionary:attributedExpression.attributes]; - if (attributedDictionary[MGLFontNamesAttribute]) { - attributedDictionary[MGLFontNamesAttribute] = @[@"literal", attributedDictionary[MGLFontNamesAttribute]]; - } - if (attributedDictionary[MGLFontColorAttribute] && [attributedDictionary[MGLFontColorAttribute] isKindOfClass:[MGLColor class]]) { - MGLColor *color = attributedDictionary[MGLFontColorAttribute]; - attributedDictionary[MGLFontColorAttribute] = color.mgl_jsonExpressionObject; + + for (NSString *key in attributedExpression.attributes.allKeys) { + attributedDictionary[key] = attributedExpression.attributes[key].mgl_jsonExpressionObject; } + } return @[jsonObject, attributedDictionary]; } |