0

I want to enable a UITextView and NSTextView with rich-text editing options (bold, italics and underline for now) across my iOS and Mac apps. I managed to figure out how to set these attributes to an NSAttributedString, and the basics of saving and retrieving NSAttributedStrings from the views, and even make them sync across devices, but I can't figure out how to make this work across devices and platforms where there might be different fonts used to render the data.

For eg. on deviceA the user has set font size 14. He/she saves some text with part of it bold, and it gets saved to NSAttributedString, which saves the whole font size with it.

self.attributedLogNote = self.textView.attributedText;

This gets converted into NSData, and synced to another device, where the reverse happens:

self.textView.attributedText = self.attributedLogNote;

But on another device, the user might have a different setting for their font size, or might be using Dynamic Type on iOS. When I set the textView with the same attributedString, it will get rendered in the font from the original device, not the font being used on this second device.

How should this be handled ideally? Is there a way to remove the font style / size / family from the NSAttributedString, but still save where the bold, italics or other properties are placed? If so, is there a system-supported way to do this, or do I have to do this myself? Or is it better to convert this back-and-forth between HTML or some other cross-platform standard for rich-text editing?

Z S
  • 7,039
  • 12
  • 53
  • 105
  • Bold/size/italic will be in the `NSFontAttributeName`, and it's inside the rtf tags (sample: `{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\froman\fcharset0 Times-Roman;}`). So you can't separate it. You can use this https://stackoverflow.com/a/41413014/1801544 to change the font according to the user preference. You could use HTML tags with only `/` too (see there: https://stackoverflow.com/questions/44027651/convert-attributed-string-to-simple-tagged-html) but then it's using RTF <=> NSAttributedString <=> HTML, seems overkilled? – Larme Feb 05 '18 at 12:36
  • Would there be a simpler way to do this, that doesn't involve converting to RTF / HTML? – Z S Feb 05 '18 at 12:51
  • This one: https://stackoverflow.com/a/41413014/1801544 It changes the font to the one you want. It can have a "HTML or RTF" init as you wish, the main logic is behind enumerating the `NSFontAttributeName`. – Larme Feb 05 '18 at 12:53
  • Thanks. Seems more complicated than I thought it would be. – Z S Feb 05 '18 at 13:05

0 Answers0