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

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

问题描述

如何在 UITextView 中进行语法高亮,特别是 Objective-C语法高亮(和检测)> 在 iPhone 上?

我对如何做到这一点的一些想法:

  • NSAttributedString.现在可在 iOS 3.2 及更高版本中使用.但是如何将 NSAttributedString 放入 UITextView 中?
  • UIWebView.当用户完成编辑并使用 CSS 样式表为文本着色时覆盖它.但是我将如何在 UIWebView 中执行此操作,为其提供文本然后对其进行着色?

解决方案

更新: 从 iOS 7 和 TextKit 开始,语法高亮变得很容易.看看这里的介绍(由我).

<小时>

假设你想要一个可编辑的组件,希望不大.快速概述,以确保涵盖所有内容:

  • UITextView:仅纯文本,没有(公共)方法.但是可编辑.没有机会改变任何东西,没有机会获得几何图形等.在内部使用网络视图.每个段落都是一个

    ,由编辑引擎更改.您可以更改那里的 DOM,但这都是私有 API.都是私人的,因此没有选择.
  • UIWebView:html,所以它可以设置样式,嵌入图像等.我猜(没有仔细研究)这就是前面提到的三个 20用户界面使用.问题:无法编辑.没有(公共)方法可以解决这个问题.如果没有私有 API 和很多麻烦,您将无法获得选择、访问 DOM 等.编辑会像在 UITextView 中一样工作,但需要一个全新的头痛程度.仍然:私人.
  • CoreText 框架:自 iOS 3.2 以来,这是一个非常出色的渲染引擎.但就是这样.可以直接布局渲染NSAttributesStrings.但是,没有用户交互.没有文本选择,没有文本输入,没有拼写检查,没有替换,没有突出显示,没有不不不.渲染而已.还需要大量工作才能在具有长文本的旧设备上快速运行.
  • UITextInput 协议:允许与键盘交互并获取文本输入.还具有基本的文本替换功能.问题:仅文本输入,记录不全.没有文本选择等(见上文).

所以我们来了.缺少中心功能的两个全功能组件和缺少缺失环节的两个部分解决方案.以下是我能想到的所有可行方法:

  • 让用户编辑没有样式的 UITextView 并显示在 UIWebView 中设置样式的内容.对于后者,您可以使用 Three20 的东西.
  • CoreTextUITextInput 之间建立缺失的链接.这包括但不限于文本选择、突出显示、背景颜色、文本存储和管理、效率、拼写检查等.其中一些由系统框架提供,但仍需要大量集成.
  • 提交错误并等待一个不错的公共 API 出现(我个人的方法).
  • 说服 Apple 再次允许私有 API 并弄乱 UItextView 的 DOM.

Omni Group 采用了第二种方法并创建了一个可编辑的文本视图.您可以在 OmniUI 框架中找到它.然而,这种实现远非完美.(至少是几个月前我上次检查的时候).他们努力尝试,但仍然没有设法在用户交互方面达到苹果的完美.Apple 肯定已经投入了至少几个人年的时间来只进行文本选择和编辑的 UI 交互.在家无事可做,即使对于像omni集团这样的公司来说也是如此.

所有这些都有一个有趣的方面:​​iWork.Apple 声称不会在其中使用任何私有 API.但是文本选择与操作系统中的完全相同.因此,他们必须将框架源代码复制到应用程序中.非常诚实的方法;)

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 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?

解决方案

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.
  • 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:

  • 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.

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

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