views:

291

answers:

2

Is there a common library, parser, etc. for Cocoa or Cocoa-Touch that can take a chunk of text and do the proper syntax highlighting? As a simple example, I'd like to have a UITextView that has C/C++ syntax highlighting.

+1  A: 

I don't know of any Objective-C or Cocoa specifc syntax highlighting libraries, but perhaps you might want to consider using a UIWebView along with one of the many javascript syntax highlighting apps to render your code. For example you could build a html document that includes the google-code-prettify engine html wrapped around your C/C++ code and hand that off to a UIWebView to render.

Jason Jenkins
Won't work as the UIWebView on the iPhone/iPad has contentEditable disabled. :(
GothAlice
@GothAlice - Can you expand on this? As I understood the question, Travis was just asking to be able to display a chunk of text with appropriate highlighting. I don't see why contentEditable would be needed. AFAIK, google-code-prettify and other js highlighters don't require contentEditable. Is the requirement to be able to do syntax highlighting of user entered code on-the-fly? If so, can you or Travis explain the use case a bit more?
Jason Jenkins
UITextView is an editable interface, thus I commented assuming editable was a requirement. As for pure syntax highlighting, see my answer, below.
GothAlice
+1  A: 

I've coded up a bit of an example application that does syntax highlighting:

http://github.com/GothAlice/Syntax

Unfortunately my goal was for an editable syntax highlighting editor, but with contentEditable disabled (I've filed a bug report with Apple; it's a feature regression) I was hooped.

GothAlice