tags:

views:

41

answers:

2

It is simple to format text with setTextFormat. But is there any affective method for retrieving all the styling that has been made on the text? getTextFormat is known but it doesn't work on text that varies in formatting. Any ideas?

A: 

This is a lot of work, to say the least.

I recommend an existing rich text editor that you can plug in to your solution, such as obedit : http://www.oblius.com/projects/obedit/ (donationware)

Edit:

Okay, in response to your comment, you code a rich text editor by implementing a styled text standard, such as text/enriched MIME content-type (http://tools.ietf.org/html/rfc1896) or, more likely, Rich Text Format (http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx). You could also use HTML or create your own syntax.

Say the user types The quick brown fox jumps over the lazy dog. User highlights "quick" and clicks the bold button (which you supply), highlights fox and changes foreground to red (with a color picker that you supply). Hidden from view, you change the value of your data field to something like The \bquick\b brown \{color:red}fox\{\color} jumps over the lazy dog.

Now the user highlights the whole line and sets it to green. You change the hidden value to \{color:green}The \bquick\b brown fox jumps over the lazy dog.\{color} Notice that you've had to parse the line to identify and remove the conflicting color markup. What if the user wants to undo that change? Have you saved the version with the red fox? When the user enters new text at the end of the line, do you consider the caret to be inside or outside of the green color tag?

When you reload the value, you again have to parse the line and then convert all the formatting to something Flash can display, stripping out the tags.

Reinventing the wheel is not much fun.

Jay
If I wanted to use an existing rich text editor, I would have a question about how to get one. But I did ask how to code a rich text editor, that is, I have a question about how to code one. In particular, I am intereted in one thing: how to retrieve all the styling that has been made on the text?
Ruman
Please shut up with your reinventing the wheel.
Ruman
@Ruman the wording of your question (before you edited it) was misleading - it looked like you wanted to create a `RichTextEditor` control (like gedit or wordpad) using flash - and Jay is trying to answer that. He is trying to help you; if you can't thank him or be nice to him, please ignore him instead of being rude.
Amarghosh
Even if I wanted to create a rich text editor (which I am doing, by the way), I did wanted to create one but not to use an existing one. That matters! Have nothing to say except for reinventing the wheel, do not say anything, 'cause there is no help in that anyway.
Ruman
By all means provide us a link to your rich text editor when you're done.
Jay
For whom did you explain things with caret and stuff? Did I asked for that? If you think you're so smart to know that, good for you. But my question wasn't anywhere about that. And even the title was somewhat misleading, the description is there to read.
Ruman
provide us a link? ... and do you talk about yourelf in plural? haha
Ruman
A: 

As you said, getTextFormat(beginIndex:int = -1, endIndex:int = -1) just returns a TextFormat object populated with formatting information for the specified range of text (Only properties that are common to the entire text specified are set in the resulting TextFormat object).

If you want the formatting information of the whole text, read the htmlText property of the TextField after calling setTextFormat. It contains the HTML representation of the text field's contents.

var str:String = "The quick brown fox jumps over the lazy dog\n";
var tf:TextField = new TextField();
tf.multiline = true;
tf.text = str + str + str;
var f:TextFormat = new TextFormat("Arial", 15, 0xFF0000, true, false, 
    true, null, null, TextFormatAlign.CENTER);
tf.setTextFormat(f, 0, str.length);
f = new TextFormat("Courier New", 12, 0x00FF00, false, false, 
    false, "http://www.google.com", null, TextFormatAlign.LEFT);
tf.setTextFormat(f, str.length, 2 * str.length);
f = new TextFormat("Times New Roman", 12, 0x0000FF, false, true, 
    true, null, null, TextFormatAlign.RIGHT);
tf.setTextFormat(f, 2 * str.length, str.length * 3);
tf.width = 400;
tf.height = 300
addChild(tf);
trace(tf.htmlText);

The output is:

<P ALIGN="CENTER">
    <FONT FACE="Arial" SIZE="15" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">
        <B>
            <U>The quick brown fox jumps over the lazy dog</U>
        </B>
    </FONT>
</P>
<P ALIGN="LEFT">
    <FONT FACE="Courier New" SIZE="12" COLOR="#00FF00" LETTERSPACING="0" KERNING="0">
        <A HREF="http://www.google.com" TARGET="">The quick brown fox jumps over the lazy dog</A>
    </FONT>
</P>
<P ALIGN="RIGHT">
    <FONT FACE="Times New Roman" SIZE="12" COLOR="#0000FF" LETTERSPACING="0" KERNING="0">
        <I>
            <U>The quick brown fox jumps over the lazy dog</U>
        </I>
    </FONT>
</P>
Amarghosh
Yes, thank you for the info, but what if one wanted to get formating separated from text, is there a way?
Ruman
If formatting varies across the text, how can you separate it from the text? If first two words of a line are bold and next two are normal, is the line bold or normal?
Amarghosh
Haha, yeah, I just thought flash is so powerful to have it done. =)
Ruman