UITextView with Syntax Highlighting
UITextView w/ Syntax Highlighting
I’ve found a question on this already but it didn’t have a complete answer to it.
Is there a Framework or Library that can do Syntax Highlighting in a
UITextView, specifically syntax highlighting (and detection) for
Objective-C on the iPhone.
Some of my Ideas of how to do this:
NSAttributedString. Now available in iOS 3.2 and greater. But how would I put a
NSAttributedStringinto a UITextView?
UIWebView. Overlay it when the user finished editing and color the text with CSS stylesheets. But how would I do this in a
UIWebView, giving it the text and then coloring it?
- Transforming a rectangle image into a quadrilateral using a CATransform3D
- Proper way of saving and loading pictures
- Printing UIImage using AirPrint causes cut-off content
- How to swap views using a swipe gesture XCode
- Customizing a UIImagePickerController
- Xcode doesn't show the line that causes a crash
7 Solutions Collect From Internet About “UITextView with Syntax Highlighting”
UPDATE: Since iOS 7 and TextKit, syntax highlighting has become easy as pie. Look no further than here for an introduction (by me).
Assuming that you want an editable component, there is not too much hope. Giving a quick overview, just to make sure I cover everything:
UITextView: plain text only, no (public) way around. But editable. No chance to alter anything, no chance to get geometry etc. Uses a web view internally. Each paragraph is a
<div>, which is altered by the editing engine. You could change the DOm in there but that’s all private API. All private, hence no option.
UIWebView: html, so it can be styled, have embedded images, etc. I guess (without looking into it) that this is what the previously mentioned Three 20 UI uses. The problem: cannot be edited. There’s no (public) way around that. You canot get selections, acces the DOM, etc without private API and a lot of headaches. Editing would work like in UITextView but require a whole new level of headache. Still: private.
CoreTextFramework: Since iOS 3.2 a very very good rendering engine. But that’s it. Can directly layout and render
NSAttributesStrings. However, there is no user interaction. No text selection, no text input, no spell checking, no replacements, no highlights, no no no no no. Just rendering. And quite a bit of work to make it fast on old devices with long texts.
UITextInputProtocol: Allows interaction with the keyboard and to grab text input. Also features basic text replacements. Problem: text input only, badly documented. No text selection etc (see above).
So here we are. Two fully functional components that lack a central function and two partial solutions that lack the missing link. Here are all viable approaches I can come up with:
- Have the user edit a
UITextViewunstyled and display the content styled in a
UIWebView. For the latter you can use the Three20 thing.
- Build the missing link between
UITextInput. This includes, but is not limited to, text selection, highlight, background colors, text storage & management, efficiency, spell checking etc. Some of these are provided by system frameworks but still need a lot of integration.
- File a bug and wait for a nice public API to come (my personal approach).
- Convince Apple to allow private API again and mess with
The Omni Group has taken the second approach and created an editable text view. You can find it in the OmniUI framework. However, this implementation is far from perfect. (at least it was when I last checked a few months ago). They tried hard but still didn’t manage to get to Apples perfection in user interaction. Apple must have invested at least a few man-years into doing only the UI-interaction with text selection and editing. Nothing to be done at home, and as it seems even for companies as the omni group.
There is a interesting aspect to all of this: iWork. Apple claims to not use any private API in them. But the text selection is the very same as in the OS. So they must have copied framework source into the apps. Very honest approach 😉
Since iOS 5 you can use a UIWebView and set
contenteditable="true" on a div in it. This makes the UIWebView open a keyboard up and allow the user to directly input text into the DOM. You can then write a webapp that does syntax highlighting. You can communicate with the cocoa app with custom URLs.
The only issue I have is you don’t seem to get spellchecking.
JTextView is a
UITextView replacement that supports an
NSAttributedString text store. It allows the display of rich text but is incomplete.
I don’t know much about syntax detection, but if you want to have multiple styles in the same UITextView, then Three 20 UI is a thing that you should look at
Here is another question that may relate to you: What is the best way to implement syntax highlighting of source code in Cocoa. I am not sure if it is Mac or iPhone but I think if you combine this and Three20, then you can have your own in iPhone. Good luck
If anybody is still looking for a complete solution for this, I know two libraries that provides this:
- Highlightr: A Swift library for syntax highlighting, supports hundreds of languages but uses JS as backend. It’s fast enough for live editing, though. (Disclaimer: I am the creator of this library).
- SyntaxKit: A native solution by Sam Soffes, but on early stages of development. Should support any TextMate syntax in the future. This should be the way to go when the library is mature enough.
- Early return/golden path in Swift
- How to get current location at the instant UIImagePickerController captures an image?
- How to use Linux openssl to generate CSR for iOS?
- UIView animations with autoreverse
- What does this LLVM 1.5 warning mean? “protocol qualifiers without 'id' is archaic”
- What are the difference and use-cases for va_list, CVaListPointer, AnyObject …, CVarArgType?
- Strange error when adding items to prototype cells in storyboard-IB
- New image name for iPhone 5
- WatchKit Upload
- Facebook login – stays at white web page SWIFT
- Most performant way to draw text on a curve, and animate it?
- How can I add forward class references used in the -Swift.h header?
- UIImageView as a link to a URL
- Xcode 5: failed to get the task for process
- Youtube in iOS5 – done button Tapped