UITextView带有语法高亮 [英] UITextView with Syntax Highlighting

查看:165
本文介绍了UITextView带有语法高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

UITextView w /语法高亮显示

这已经,但它没有一个完整的答案。

I've found a question on this already but it didn't have a complete answer to it.

是否有框架或库可以在 UITextView 中执行语法高亮显示, / strong>上的 Objective-C 的(和检测)。

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.

我的一些想法如何做:


  • NSAttributedString 。现在可在iOS 3.2和更高版本。但是如何将 NSAttributedString 放入UITextView?

  • UIWebView 。当用户完成编辑并用CSS样式表为文本着色时,覆盖它。但是我该如何在 UIWebView 中这样做,给它的文本,然后着色呢?

  • NSAttributedString. Now available in iOS 3.2 and greater. But how would I put a NSAttributedString into 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?

推荐答案

更新:从iOS 7和 TextKit ,语法高亮变得容易。 请不要在这里查看介绍(通过我)。

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 纯文本,无(公共)方式。但可编辑。没有机会改变任何东西,没有机会获得几何等。在内部使用Web视图。每个段落是由编辑引擎更改的< div> 。你可以改变里面的DOM,但这是所有的私人API。

  • UIWebView html ,所以它可以是样式,图像等。我猜(不研究它),这是前面提到的三20 UI使用。问题:无法编辑。没有(公共)方式。你可以获得选择,访问DOM等,没有私人API和很多头痛。编辑将像在UITextView中工作,但需要一个全新的水平的头痛。

  • CoreText 框架:由于iOS 3.2是一个非常好的渲染引擎。但是就是这样。可以直接布局和渲染 NSAttributesString s。但是,没有用户交互。没有文本选择,没有文本输入,没有拼写检查,没有替代,没有高亮,没有没有没有没有。只是渲染。

  • UITextInput 协议:允许与键盘进行交互,抓取文本输入。还具有基本的文本替换。问题:仅文本输入,记录不良。没有文字选择等(见上文)。

  • 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.
  • CoreText Framework: 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.
  • UITextInput Protocol: 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:


  • 让用户编辑 UITextView uncyled,并在 UIWebView 中显示样式的内容。 c> UITextInput code>。这包括但不限于文本选择,高亮显示,背景颜色,文本存储和存储。管理,效率,拼写检查等。其中一些是由系统框架提供的,但仍然需要大量的集成。

  • 提出一个错误,等待一个好的公共API来

  • 说明Apple再次允许私人API并使用 UItextView 的DOM。

  • Have the user edit a UITextView unstyled and display the content styled in a UIWebView. For the latter you can use the Three20 thing.
  • Build the missing link between CoreText and 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 UItextViews DOM.

Omni Group采用了第二种方法,并创建了一个可编辑的文本视图。您可以在 OmniUI 框架中找到它。然而,这种实现远非完美。 (至少是我上次检查几个月前)。他们尝试努力,但仍然没有设法得到苹果完美的用户交互。苹果必须投入至少几个人年才能做的只是与文本选择和编辑的UI交互。没有什么可以在家里做,因为它似乎甚至为公司作为全方位组。

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.

这一切都有一个有趣的方面:​​iWork。苹果声称不使用任何私有API。但是文本选择与操作系统中的选择是一样的。所以他们必须将框架源代码复制到应用程序中。非常诚实的方法;)

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带有语法高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆