创建自定义文本绘图视图 [英] Creating a custom text-drawing view

查看:86
本文介绍了创建自定义文本绘图视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为iPhone创建一个语法荧光笔,为了显示多种格式的文本,我有子类UIView和修改 drawRect:方法,使每个行显示有正确的语法高亮显示(高亮显示早于RegEx,文本用 CGContextShowTextAtPoint()一次绘制一行)。一切工作正常,我存储每行文本作为NSString在NSMutableArray,我通过隐藏的UITextField和它的委托方法处理键盘,光标是一个闪烁的CALayer,可以移动与触摸,我有一个自定义滚动视图处理滚动。但是,我有两个问题,我似乎无法围绕我的头:

I am creating a syntax highlighter for the iPhone and in order to display text with multiple formats, I have sub-classed UIView and modified the drawRect: method so that each line is displayed with the proper syntax highlighting (highlighting is done earlier with RegEx, text is drawn with CGContextShowTextAtPoint() one line at a time). Everything works ok, I store each line of text as an NSString in an NSMutableArray, I handle the keyboard through a hidden UITextField and its delegate methods, the cursor is a blinking CALayer that can be moved around with touches and I have a custom scroll view that handles scrolling. However, I have two problems that I can't seem to wrap my head around:


  1. 只是继续离开屏幕的左端。为了保持快速,我只重新绘制视图的部分已经改变(通常只是正在编辑的行,但有时下面的行以及例如,如果你按回到文档的一半)与 setNeedsDisplayInRect: / code>。这使得换行复杂,因为你必须在屏幕上绘制多行,即使它仍然只是数组中的一个对象。

  1. Word wrap, right now the text just keeps going off the left end of the screen. To keep things fast I only redraw the portions of the view that have changed (usually just the line being edited, but sometimes the lines below as well e.g. if you press return halfway through the document) with setNeedsDisplayInRect:. This makes word wrap complicated because then you have to draw more than one line on the screen, even though it still is only one object in the array.

UIViews具有1024x1024的最大内容大小,其等于大约64行。我需要显示更多的能力。我正在考虑使用多个CALayers一个接一个,但我有麻烦绘制内容的层(使用 drawLayer:inContext: drawInContext: / code>)。

UIViews have a maximum content size of 1024x1024 which equates to about 64 lines. I need the ability to display more than that. I am thinking about using multiple CALayers one after another, but I am having trouble drawing content to the layers (using drawLayer:inContext: and drawInContext:).

所以我的问题是:


  • 任何人都有任何建议,甚至一般的建议,如何完成这两点。

感谢,

Kyle

编辑:滚动问题几乎解决了,但是我仍然有麻烦与word包装。我的麻烦是一切都是通过行来完成的:视图一次更新一行,文本存储为一个行数组,荧光笔一次高亮显示一行等,并且在数组中有一个索引(一个文本行)占用屏幕上的多行引起一些问题,例如,我不得不实现自己的可移动光标,当您移动光标时,它需要能够转动显示行(通过将touch.x除以行高)转换为文本行(数组中的索引)。任何想法?

The scrolling problem is pretty much solved, however I am still having trouble with word-wrap. My trouble is that everything is done by line: the view updates one line at a time, the text is stored as an array of lines, the highlighter highlights one line at a time, etc. and having a single index in the array (one line of text) take up multiple lines on the screen raises some problems, for example, I had to implement my own movable cursor and when you move the cursor it needs to be able to turn a display line (found by dividing touch.x by the line height) into a text line (an index in the array). Any ideas?

推荐答案

Three20 library 。不知道它如何符合您的目标,但可能为您服务为例。 (图书馆本身有点。肿,但是是一个奇妙的源头看。)

Check out TTStyledText in Three20 library. Not sure how well it matches your goals, but might serve you as an example. (The library itself is a bit bloated, but is a wonderful source to look at.)

这篇关于创建自定义文本绘图视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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