Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

How to dynamically replace substring in MGLSymbolStyleLayer text? #233

Closed
martinhoeller opened this issue Mar 20, 2020 · 1 comment
Closed

Comments

@martinhoeller
Copy link

I am drawing custom text labels using a MGLSymbolStyleLayer which was populated with features from GeoJSON. The features contain a property called displayName which I can render just fine using layer.text = NSExpression(forKeyPath: "displayName").
The displayName property also contains a special character to indicate a line break, for example the value could be "Hello|World".

How can I create an NSExpression that replaces | with \n?

I already tried with an NSString category and NSExpression(format: "FUNCTION(..)"), but I always get an exception saying that the function is unknown.

Configuration

Mapbox SDK versions:
Mapbox-iOS-SDK 5.7.0

iOS/macOS versions:
iOS 13.2

@1ec5
Copy link
Contributor

1ec5 commented Apr 13, 2020

How can I create an NSExpression that replaces | with \n?

String replacement inside expressions is currently unsupported: mapbox/mapbox-gl-js#4100. For now, the best workaround is to modify the GeoJSON so that each feature’s displayName property contains newlines instead of the | character. (Or you could use a different feature property for that value.)

If you’re currently loading the GeoJSON into a shape source using MGLShape(data:encoding:error:), you could use JSONSerialization to first convert the structure to a Dictionary that you can manipulate. Or, if you have full control over the contents of the GeoJSON, you could edit the GeoJSON ahead of time so no manipulation is needed at runtime.

I already tried with an NSString category and NSExpression(format: "FUNCTION(..)"), but I always get an exception saying that the function is unknown.

Under the hood, the SDK converts NSExpressions and NSPredicates to a Mapbox-specific data structure based on the Mapbox Style Specification, so it isn’t generally possible for application code to extend this system with additional functions.

@knov knov closed this as completed Apr 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants