开发者

Flex TLF - Ability to create links using regex matches?

When importing / converting text into a TextFlow, is there a way to define RegEx based parsers that generate elements?

For example:

  • I want to create a LinkElement whenever an email address is matched.
  • I wa开发者_开发技巧nt to create a InlineGraphicElement whenever a smiley :) is matched

Regards,

Marty


Easiest way I know of is to take the text and use a sequence of regex to embed HTML. Then create a TextFlow from the HTML and assign it to the RichEditableText (or whatever component you are using). Something like:

var emailRE:RegExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var smileyRE:RegExp = /:\)/g;
var sample:String = "Email john@doe.com with smiley :) example.";

var result:String = '';
while (emailRE.test(sample)) {
    result = sample.replace(emailRE, "<a href='event:emailClicked'><font color='0x0000DD'>$&</font></a>");
}
if (result.length > 0) {
    sample = result;
}

var result2:String = '';
while (smileyRE.test(sample)) {
    result2 = sample.replace(smileyRE, "<img src='smiley.png'/>");
}
if (result2.length > 0) {
    sample = result2;
}

richEditableTextComponent.textFlow = TextConverter.importToFlow(sample, TextConverter.TEXT_FIELD_HTML_FORMAT);
richEditableTextComponent.textFlow.addEventListener("emailClicked", emailClickedHandler, false, 0, true);

Then the click handler would be something like:

private function emailClickedHandler(event:FlowElementMouseEvent):void {
    var emailAddress:String = LinkElement(event.flowElement).getFirstLeaf().text;
    // do something with emailAddress
}

You can also manually build a TextFlow based on a string by parsing the string for emails/smileys and building SpanElement, LinkElement, and InlineGraphicElement, that's much more cumbersome to the programmer, though, however I have seen a need for it (especially with RichEditableText and having to parse each SpanElement as the user types a key to see if smileys are matched and replace with graphics; using HTML here wouldn't help because you wouldn't want to reassign the whole TextFlow object since it would cause a full redraw of the whole flow instead of only updating what has to be updated, such as a single SpanElement getting split into a sequence of SpanElement - InlineGraphicElement - SpanElement).

Hope this helps!

Justin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜