0

I have an app that exports Objective-C and Swift code.

In the preview window for this auto generated code I would like to format the text with the same colours the default theme of Xcode uses i.e. green for comments, blue for numbers, red for strings purple for class names etc.

Apps such as PaintCode manage to achieve this. Does anyone know of a library or other means of producing this.

Paint Code with formatted code preview

Mark Bridges
  • 8,228
  • 4
  • 50
  • 65

2 Answers2

2

You will need a lexer to split the text into parts: identifiers, symbols, string and number literals, comments, etc. Further reading about lexers on Wikipedia.

Then you have to know what color to apply on them, including identifiers of different kind: keywords, functions, variables, types, etc. For those, you can manage some kind of lists and look up them.

For parsing, a good place to start is NSScanner or simply go over the text character by character. That’s how we do it in PaintCode.

For coloring itself, simply use NSAttributedString and NSForegroundColorAttributeName. There’s no need to go into Core Text.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
-1

Have a look at CoreText if you are working with Objective C or Swift.

You can color every single drawn glyph.

Apple Developer Documentation about CoreText

Maurice
  • 1,466
  • 1
  • 13
  • 33