This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
Add NSExpression operator for rich text (format expressions) #12739
Labels
feature
GL JS parity
For feature parity with Mapbox GL JS
gl-ios
iOS
Mapbox Maps SDK for iOS
macOS
Mapbox Maps SDK for macOS
runtime styling
The iOS and macOS map SDKs’ NSExpression implementation will automatically support the
format
expression support being added in #12624, via theMGL_FUNCTION
expression operator. However, this operator is only a last-ditch fallback; for something as amenable to runtime styling as formatted text fields, we would want to provide a more ergonomic representation when working with NSExpression.During early design discussions for the formatted string feature, I had proposed possibly piggybacking on NSAttributedString as a familiar platform-specific representation for the style specification’s Formatted type. NSAttributedString was attractive because of its numerous methods for applying and inspecting attributes. It seemed natural that a rich text object would be represented by the platform’s standard rich text class.
Unfortunately, NSAttributedString didn’t turn out to be a good fit for the
format
expression for a couple reasons:format
operator’s annotation objects currently supportfont-scale
andtext-font
properties that bear no relation toNSAttributedStringKey
. It would be no problem to define ad-hoc attribute keys likeMGLFontScaleAttributeName
that are set to NSExpressions, but the lack of support forNSFontAttributeName
could be surprising to developers.The obvious alternative to NSAttributedString would be a more literal translation of the JSON, an aftermarket expression operator like
mgl_attributed({…})
. (The term “format” should be avoided for its similarity to-[NSString stringWithFormat:]
, and “annotation” could be confused with MGLAnnotation.) The main problem with the literal approach is that NSExpression’s format string syntax lacks dictionary literals, so the annotation objects would have to be represented by%@
placeholders in most cases.Perhaps it would be too ambitious to come up with an ergonomic, one-to-one representation of all Formatted obects in NSExpression. Maybe it would be OK to convert
format
JSON expressions toMGL_FUNCTION
NSExpressions.If so, I’d recommend implementing very basic NSAttributedString support only as a convenience when going from NSExpression to JSON. Besides supporting
MGLFontScaleAttributeName
andMGLFontNamesAttributeName
attributes, we could helpfully convert anyNSFontAttributeName
to the appropriatefont-scale
andtext-font
annotation arguments. (font-scale
might be difficult because the overall expression doesn’t contain the base font size.) We could also extend NSTextAttachment to make it easier to put an expression inside an NSAttributedString./cc @fabian-guerra @ChrisLoer
The text was updated successfully, but these errors were encountered: