[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.
This commit is contained in:
Fabian Guerra Soto
2019-03-22 13:45:39 -07:00
committed by GitHub
parent c78b60bf35
commit 1d7b243a06
9 changed files with 75 additions and 78 deletions

View File

@@ -439,7 +439,7 @@ can use to update the formatting of `MGLSymbolStyleLayer.text` property.
In style JSON | In Objective-C | In Swift
--------------|-----------------------|---------
`text-font` | `MGLFontNamesAttribute` | `.fontNamesAttribute`
`font-scale` | `MGLFontSizeAttribute` | `.fontSizeAttribute`
`font-scale` | `MGLFontScaleAttribute` | `.fontScaleAttribute`
`text-color` | `MGLFontColorAttribute` | `.fontColorAttribute`
See the “[Predicates and Expressions](predicates-and-expressions.html)” guide for

View File

@@ -550,9 +550,9 @@ with the `MGLSymbolStyleLayer.text` property.
Key | Value Type
--- | ---
`MGLFontNamesAttribute` | `NSArray<NSString *>*`
`MGLFontSizeAttribute` | `NSNumber`
`MGLFontColorAttribute` | `UIColor` or `NSColor` on macos
`MGLFontNamesAttribute` | An `NSExpression` evaluating to an `NSString` array.
`MGLFontScaleAttribute` | An `NSExpression` evaluating to an `NSNumber` value.
`MGLFontColorAttribute` | An `NSExpression` evaluating to an `UIColor` (iOS) or `NSColor` (macOS).
This function corresponds to the
[`format`](https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-types-format)

View File

@@ -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

View File

@@ -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]));

View File

@@ -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];
}

View File

@@ -550,10 +550,10 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
let redColor = UIColor.red
#endif
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)
//#-end-example-code

View File

@@ -992,16 +992,16 @@ using namespace std::string_literals;
{
MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"]
fontNames:nil
fontSize:@(1.2)];
fontScale:@(1.2)];
MGLAttributedExpression *attribute2 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"biz"]
fontNames:nil
fontSize:@(1.0)];
fontScale:@(1.0)];
MGLAttributedExpression *attribute3 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"bar"]
fontNames:nil
fontSize:@(0.8)];
fontScale:@(0.8)];
MGLAttributedExpression *attribute4 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"\r"]
fontNames:@[]
fontSize:nil];
fontScale:nil];
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@, %@, %@, %@)",
MGLConstantExpression(attribute1),
MGLConstantExpression(attribute4),
@@ -1014,16 +1014,16 @@ using namespace std::string_literals;
{
MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"]
fontNames:nil
fontSize:@(1.2)];
fontScale:@(1.2)];
MGLAttributedExpression *attribute2 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"biz"]
fontNames:nil
fontSize:@(1.0)];
fontScale:@(1.0)];
MGLAttributedExpression *attribute3 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"bar"]
fontNames:nil
fontSize:@(0.8)];
fontScale:@(0.8)];
MGLAttributedExpression *attribute4 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"\n"]
fontNames:@[]
fontSize:nil];
fontScale:nil];
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@, %@, %@, %@)",
MGLConstantExpression(attribute1),
MGLConstantExpression(attribute4),
@@ -1036,7 +1036,7 @@ using namespace std::string_literals;
{
MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"]
fontNames:nil
fontSize:@(1.2)];
fontScale:@(1.2)];
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
NSExpression *compatibilityExpression = [NSExpression expressionForFunction:@"mgl_attributed:" arguments:@[MGLConstantExpression(attribute1)]];
@@ -1048,8 +1048,8 @@ using namespace std::string_literals;
}
{
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
attributes:@{ MGLFontSizeAttribute: @(1.2),
MGLFontColorAttribute: @"yellow"}] ;
attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
MGLFontColorAttribute: MGLConstantExpression(@"yellow") }] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
NSExpression *compatibilityExpression = [NSExpression expressionForFunction:@"mgl_attributed:" arguments:@[MGLConstantExpression(attribute1)]];
@@ -1068,22 +1068,25 @@ using namespace std::string_literals;
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
NSExpression *fontNames = [NSExpression expressionForAggregate:@[ MGLConstantExpression(@"DIN Offc Pro Bold"), MGLConstantExpression(@"Arial Unicode MS Bold") ]];
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
attributes:@{ MGLFontSizeAttribute: @(1.2),
MGLFontColorAttribute: @"yellow",
MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
MGLFontColorAttribute: MGLConstantExpression(@"yellow"),
MGLFontNamesAttribute: fontNames
}] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
NSArray *jsonExpression = @[ @"format", @"foo", @{ @"font-scale": @1.2, @"text-color": @"yellow" , @"text-font" : @[ @"literal", @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]]} ];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
NSExpression *exp = [NSExpression expressionWithMGLJSONObject:jsonExpression];
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
NSExpression *fontNames = [NSExpression expressionForAggregate:@[ MGLConstantExpression(@"DIN Offc Pro Bold"), MGLConstantExpression(@"Arial Unicode MS Bold") ]];
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
attributes:@{ MGLFontSizeAttribute: @(1.2),
MGLFontColorAttribute: [MGLColor redColor],
MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
MGLFontColorAttribute: MGLConstantExpression([MGLColor redColor]),
MGLFontNamesAttribute: fontNames
}] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
@@ -1092,10 +1095,11 @@ using namespace std::string_literals;
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
NSExpression *fontNames = [NSExpression expressionForAggregate:@[ MGLConstantExpression(@"DIN Offc Pro Bold"), MGLConstantExpression(@"Arial Unicode MS Bold") ]];
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"CAST(x, 'NSString')"]
attributes:@{ MGLFontSizeAttribute: @(1.2),
MGLFontColorAttribute: [MGLColor redColor],
MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
MGLFontColorAttribute: MGLConstantExpression([MGLColor redColor]),
MGLFontNamesAttribute: fontNames
}] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
@@ -1106,16 +1110,16 @@ using namespace std::string_literals;
{
MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"]
fontNames:nil
fontSize:@(1.2)];
fontScale:@(1.2)];
MGLAttributedExpression *attribute2 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"biz"]
fontNames:nil
fontSize:@(1.0)];
fontScale:@(1.0)];
MGLAttributedExpression *attribute3 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"bar"]
fontNames:nil
fontSize:@(0.8)];
fontScale:@(0.8)];
MGLAttributedExpression *attribute4 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"\n"]
fontNames:@[]
fontSize:nil];
fontScale:nil];
NSExpression *expression = [NSExpression mgl_expressionForAttributedExpressions:@[MGLConstantExpression(attribute1),
MGLConstantExpression(attribute4),
MGLConstantExpression(attribute2),

View File

@@ -426,7 +426,7 @@ can use to update the formatting of `MGLSymbolStyleLayer.text` property.
In style JSON | In Objective-C | In Swift
--------------|-----------------------|---------
`text-font` | `MGLFontNamesAttribute` | `.fontNamesAttribute`
`font-scale` | `MGLFontSizeAttribute` | `.fontSizeAttribute`
`font-scale` | `MGLFontScaleAttribute` | `.fontScaleAttribute`
`text-color` | `MGLFontColorAttribute` | `.fontColorAttribute`
See the “[Predicates and Expressions](predicates-and-expressions.html)” guide for

View File

@@ -419,7 +419,7 @@ can use to update the formatting of `MGLSymbolStyleLayer.text` property.
In style JSON | In Objective-C | In Swift
--------------|-----------------------|---------
`text-font` | `MGLFontNamesAttribute` | `.fontNamesAttribute`
`font-scale` | `MGLFontSizeAttribute` | `.fontSizeAttribute`
`font-scale` | `MGLFontScaleAttribute` | `.fontScaleAttribute`
`text-color` | `MGLFontColorAttribute` | `.fontColorAttribute`
See the “[Predicates and Expressions](predicates-and-expressions.html)” guide for