views:

511

answers:

3

I am trying to implement a control to edit text that will display the text in multiple colors. None of the solutions I have attempted yet have been good enough.

  1. UITextView cannot accomplish this. All of the text must be the same color.
  2. Using CoreGraphics to draw the text does not allow the text to be selected.
  3. Using a UIWebView, DIV and PRE tags cannot be set to contentEditable on Mobile Safari.
  4. Currently playing with using an off-screen TEXTAREA and an on-screen DIV to show the rendered text. This works pretty well, except supporting all of these at the same time seems impossible: click-to-type, click-to-move-cursor, click-and-hold-select/copy/paste.

Anyone have any tips on this predicament?

I've been trying to find any preexisting library out there that will accomplish this in a good way, to no luck. I'm open to any ideas!

+2  A: 

Well, just pulling an idea out of my... let's say hat.

Could you put a transparent UITextfield over a view that draws the text? If the background was clear and the text color was clear the user could not perceive it but it should still respond to all commands. As the user enters and edits text you could draw the results on the view underneath.

I think the selection would work without any modification at all. When the user selected the clear text, it should create the illusion of selecting the drawn text automatically.

TechZen
That gets close, although scrolling becomes an issue. How would you keep the two text panes in sync with each other while scrolling?
Alex
A: 

Like this one? StyledText It's in Three20 .

dwery
Whops. You said edit :(
dwery
A: 

Here is an idea. I have no idea if it would work.

If you are only using colors, and not styles, the a UIWebView with colored text might layout text in exactly the same way as a UITextView. You could put a UITextView with invisible ink (text and background fully transparent) over a UIWebView and mirror the contents with colors in the html. I assume you can do scrolling with javascript along with the colored layout.

drawnonward
Somehow, I missed that TechZen had given the exact same answer until after I posted my answer.
drawnonward