Flash TextField selectable bug block TextEvent.Link solution

There is an old version Felx SDK bug(in my case it‘s Flex SDK v3.3.0.4852) that when TextField.selectable is set to false, link event on the textfield will be blocked. So if you have added html text including a link into the textfield, e.g.:

var textField:TextField = new TextField();
textField.htmlText = "This is a testing message. <a href=‘event:clickHandler‘>Click</a> to see more.";
textField.selectable = false;  

Then when you click "Click", it will not trigger "clickHandler" function. This will also happen when the anchor‘s href value is a URL, e.g.:

var textField:TextField = new TextField();
textField.htmlText = "This is a testing message. <a href=‘http://www.google.com‘ target=‘_blank‘>Click</a> to see more.";
textField.selectable = false;  

When click "Click", browser will not open the Google page in a new tab. But you can actually right click on the link and select "Open" to open it.

The previous two situations are both caused by selectable property set to false using Flex SDK 3.3.0.4852. I have three solutions to fix this:

1. Set selectable to true;

2. Change Flex SDK to newly version;

3. Register event listener to handle click event;

The first two solutions are simple, I‘ll give some more details on the third solution which will not change selectable attribute nor SDK. The main idea is register MouseClick event on the textfield, when user click the text, check whether the position is on the link. If the mouse click is on the link text, use "navigateToURL" function to open the link.

1. New a TextField object and set accordingly:

var textField:TextField = new TextField();
textField.htmlText = "Testing message. <a href=‘http://www.google.com‘ target=‘_blank‘>Click</a> to see more.";
textField.selectable = false;
var textFmtLink:TextFormat = new TextFormat();
formatter.color = "0x3366BB";
formatter.underline = true;  

2. Use "formatFieldLinkText" function to formate the link text and register event listener to handle click event:

    formatFieldLinkText(textField, textFmtLink);  

   protected function formatFieldLinkText(textField:TextField, linkFormat:TextFormat):void
 {
    var fieldText:String = textField.text;
    var textFormat:TextFormat = textField.getTextFormat();
    var formatedText:String = "";
    var linkObjArray:Array = [];  

    var textArray:Array = fieldText.split(/(<a\s[^>]+>.+?<\/a>)/gi );
    var linkTextPattern:RegExp = new RegExp("(?i)[^>]*(?=</a>)" );
    var linkUrlPattern:RegExp = /href\s*=\s*[‘"]([^‘"]+)[‘"]/i;
    var linkTargetPattern:RegExp = /target\s*=\s*[‘"]([^‘"]+)[‘"]/i ;
    for (var i:int = 0; i < textArray.length; i++)
    {
        //plain text
        if (textArray[i].toString().search(/(<a\s[^>]+>.+?<\/a>)/gi )==-1)
        {
            formatedText+=textArray[i].toString();
            continue;
        }  

        var linkText:String = linkTextPattern.exec(textArray[i]);
        // check if linkText is blank
        if (isBlank(linkText))
        {
            return;
        }
        var linkUrl:Array = linkUrlPattern.exec(textArray[i]);
        var linkTarget:Array = linkTargetPattern.exec(textArray[i]);
        if (linkUrl==null)
        {
            return;
        }
        var linkObj:Object = new Object();
        linkObj.href = linkUrl== null?"" :linkUrl[1];
        linkObj.target = linkTarget== null?"" :linkTarget[1];
        linkObj.linkBegin = formatedText.length;
        linkObj.linkEnd = formatedText.length + linkText.length;
        linkObjArray.push(linkObj);  

        formatedText+=linkText;  

        textField.addEventListener(MouseEvent.CLICK, hyperLinkClicked);
    }
    textField.text = formatedText;
    textField.setTextFormat(textFormat);
    for (var j:int = 0; j < linkObjArray.length; j++)
    {
        textField.setTextFormat(linkFormat, linkObjArray[j].linkBegin, linkObjArray[j].linkEnd);
    }  

    var href:String = "";
    var target:String = "";
    function hyperLinkClicked (e:MouseEvent):void
    {
        var idx:int = e.currentTarget.getCharIndexAtPoint(e.localX, e.localY);
        if (posOnLink(idx))
        {
            var request:URLRequest = new URLRequest(href);
            navigateToURL(request, target);
        }
    }  

    // can optimize the search method
    function posOnLink(idx:int):Boolean
    {
        for (var k:int = 0; k < linkObjArray.length; k++)
        {
            if (idx >= linkObjArray[k].linkBegin && idx < linkObjArray[k].linkEnd)
            {
                href = linkObjArray[k].href;
                target = linkObjArray[k].target;
                return true ;
            }
        }
        return false ;
    }
}  

(本文系从原博客迁移至此,并进行部分编辑。原文链接:http://thewaychung.iteye.com/blog/1974209)

时间: 2024-11-04 15:56:55

Flash TextField selectable bug block TextEvent.Link solution的相关文章

[bug] VS2013 Brower Link和Aspnetpager引发的问题分析

概述 在ie11上浏览页面的时候,突然发现在使用Aspnetpager的页面会有一个bug. 开发环境:win8.1+vs2013+ie11. 项目描述:这个问题出现在内容页中,应用了母版页. 解决方案 查了很多解决方案,最后将vs2013中 将该勾取消,再次浏览,你会发现就正常了,虽然该功能很强大,但是并不是什么时候都需要.希望对你有所帮助. 既然问题出了,那就得问一个为什么? 首先得知道什么是 Browser Link ? 这个 Browser Link 的功能就是通过一个脚本文件架起流程器

TextField link事件

package { import flash.display.MovieClip; import flash.text.TextField; import flash.events.TextEvent; public class TextFildTest extends MovieClip{ public function TextFildTest() { // constructor code var textFiled:TextField = new TextField(); textFil

用cocos2d-html5做的消除类游戏《英雄爱消除》(2)——Block设计实现

Block可以说是这个游戏的核心类,它除了包含自身的一些属性和方法外还添加了对触摸事件的响应. 我们先来看下源码吧 /** * Power by html5中文网(html5china.com) * author: jackyWHJ */ var Block = cc.Sprite.extend({ id:0, name:"", active:true, pointX:0, pointY:0, beginPoint:null, flash:false, ctor:function (a

【详解】如何编写Linux下Nand Flash驱动

From: http://www.crifan.com/files/doc/docbook/linux_nand_driver/release/html/linux_nand_driver.html 版本:v2.2 Crifan Li 摘要 本文先解释了Nand Flash相关的一些名词,再从Flash硬件机制开始,介绍到Nand Flash的常见的物理特性,且深入介绍了Nand Flash的一些高级功能,然后开始介绍Linux下面和Nand Flash相关的软件架构MTD的相关知识,最后介绍了

nand Flash原理

6.1.3) Block 块 一个 Nand Flash (的 chip,芯片) 由很多个块(Block)组成,块的大小一般是 128KB, 256KB, 512KB,此处是 128KB.其他的小于 128KB 的,比如 64KB,一般都是下面将要介绍到的small block的Nand Flash. 块 Block,是Nand Flash的擦除操作的基本/最小单位. 6.1.4) Page 页 每个块里面又包含了很多页(page) .每个页的大小,对于现在常见的Nand Flash多数是2KB

JDBC Tutorials: Commit or Rollback transaction in finally block

http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Commit or Rollback transaction in finally block In most of JDBC books, the transaction management idiom that is followed is, after executing the update s

textField各个属性和方法

今天有需要用到TextField,因为没有自能提示,我再网上找到了一位高手的总结算是作为搜藏,与大家分享 TextField对象相关的属性和方法,内容十分丰富,下面几个表格: 表一 TextField 对象的方法 方法 说明TextField.addListener 加入接收触发事件如文本域内容变化或滚动变化的监听对象,触发事件可以参看最后一个表.TextField.getDepth 返回文本域的Depth深度.TextField.getNewTextFormat 获得最新加入文本的默认文本格式

jinja2对block模板的理解

模板继承包含基本模板和子模板.其中基本模板里包含了你这个网站里的基本元素的基本骨架,但是里面有一些空的或者是不完善的块(block)需要用子模板来填充. 如基本模板base.html为: <!doctype html public "-//w3c//dtd html 4.01//en"> <html lang="en"> <html xmlns="http://www.w3.org/1999/xhtml"> &

flash

Nand flash编辑 本词条缺少信息栏.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! Nand-flash内存是flash内存的一种,其内部采用非线性宏单元模式,为固态大容量内存的实现提供了廉价有效的解决方案.Nand-flash存储器具有容量较大,改写速度快等优点,适用于大量数据的存储,因而在业界得到了越来越广泛的应用,如嵌入式产品中包括数码相机.MP3随身听记忆卡.体积小巧的U盘等. 目录 1简介 2NOR与NAND的区别 ? 性能比较   ? 接口差别   3NAND