Converting macOS system colors to iOS system colors in NSAttributedString

When syncing an NSAttributedString between devices, it seems that macOS system colors are decoded as "UIExtendedGrayColorSpace" black color on iOS. For example, if I have text on the Mac and chose systemColorRed, this is converted to UIExtendedGrayColorSpace black on iOS.

I would like to convert the macOS system red (for example) to the iOS system red, but I'm not sure the best way to do this.

My question is: has anyone discovered an efficient and clean way to convert macOS system colors to iOS system colors when syncing between devices?

I have seen that the system colors on both devices have a consistent hex value when reading the color components. A possible solution would be to save the hex value of the colors in the attributed string. We could then enumerate the hex values and exchange the attributed string foreground color for the appropriate system color of whatever device is being used. I would greatly appreciate thoughts regarding this.

Thank you everyone!

To clarify: the user is able to select the text color in the text view's attributed string. So if the user selected the adaptive red color, I would like it to be adaptive red on both macOS and iOS.

I don't know of any API that's going to do exactly what you're asking for. I think you're going to have to make an adjustment "manually" when crossing between platforms, and there are two approaches that seem reasonable.

  1. Since the list of colors with symbol names (including the adaptive system colors and things like controlDisabledTextColor on macOS) doesn't match across platforms, you'll have to make a list of colors that you want to adjust. On each platform, you'll have to look in the attributes to find these colors, and rewrite the attributes with a platform-specific color name. You could provide substitutes for colors that are not named on one or other source platform if you wanted to go to the trouble.

  2. Or, you can transition through a CGColor that is specified in terms of (say) RGB components along with a colorspace name. Probably P3 would be a natural choice of color space to use as "common currency" for these conversions.

Now, on each platform, you can try to convert a NSColor or UIColor to a CGColor with RGB+colorspace, but not all conversions will produce a result. For those that do, you can use the resulting CGColor on the other platform. For those that don't, you're going to have to fall back to some heuristic to decide what color to use instead — not an easy problem to solve in general.

I'd strongly urge you to submit an enhancement request via Feedback Assistant for API that would ease transitioning colors between platforms. This seems like useful functionality to have.

Converting macOS system colors to iOS system colors in NSAttributedString
 
 
Q