UITextView with Syntax Highlighting(带有语法高亮的 UITextView)
问题描述
How to 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 aNSAttributedStringinto a UITextView?UIWebView. Overlay it when the user finished editing and color the text with CSS stylesheets. But how would I do this in aUIWebView, giving it the text and then coloring it?
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 renderNSAttributesStrings. 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 aUIWebView. For the latter you can use the Three20 thing. - Build the missing link between
CoreTextandUITextInput. 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
UItextViews DOM.
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 ;)
这篇关于带有语法高亮的 UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有语法高亮的 UITextView
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
