Syntax Highlighting in Cocoa OS X

I'm wondering how I would have syntax highlighting in a text view.

Better off letting the user edit a UITextView, and then display the content with syntax highlighting in a UIWebView, once the user has finished editing.

There may be a better way to do this, but you can make a class specifically for syntax highlighting. Feed it a text view to watch for changes. You can store keywords in an array and use NSScanner to dip through the text view's string, grabbing the ranges that need to be highlighted. If you are using NSTextView, add temporary attributes to the ranges of text that should be highlighted.


Maybe because I hate the HTML/CSS spaghetti pattern but bringing a web view into the mix for syntax highlighting sounds like a nightmare. I know web views were used on iOS before attributed strings were made available, but I think you're better off using attributed strings.

Hi,

try find form Apples videos there are an example done with highlighting the background with javascript there are source code too.

I'm sorry but don't remeber what year it was or the name.

Hi,

I found it.

Little help to you source code is ColorMyWords and year might be 2013 for the video.

Link to video https://developer.apple.com/videos/play/wwdc2013-615/

Take a look at TextKit. Sam Soffes did an excellent screencast around this. It's subscribers only but there's some good sample code available without a subscription you can look at:


http://nsscreencast.com/episodes/208-syntax-highlighting-with-textkit

Syntax Highlighting in Cocoa OS X
 
 
Q