关于text引擎

    这不是一篇介绍text引擎的,只是提出一个text引擎的使用方法。想要实现的效果很简单,就是在一定的宽度内,将一段文本显示出来,部分有下划线,移上去可点击,弹出相应的提示信息。。用的是text引擎。

做的时候麻烦来了。不知道要怎么加下划线。TextElement也没有这东西。后来想了个办法,用GraphicElement,封装一个TextField.

    var e1:TextElement = new TextElement('Consider, what makes a text line a ', new ElementFormat(new FontDescription(), 24));
    
      var e3:TextElement = new TextElement('?', new ElementFormat(new FontDescription(), 24));
      var e:Vector.<ContentElement> = new Vector.<ContentElement>();
      e.push(e1, e2(),e3);
      
      
      function e2():GraphicElement{
          var txt:TextField=new TextField();
          txt.text="textline";
         var  textFormat:TextFormat = new TextFormat();
         textFormat.underline=true;
         txt.setTextFormat(textFormat);
          var gee:GraphicElement=new GraphicElement(txt,30,20,new ElementFormat(new FontDescription(), 24));
          return gee;
      }
       var block:TextBlock = new TextBlock(new GroupElement(e));
      var line:TextLine = block.createTextLine(null, stage.stageWidth);

      var _y:Number = 0;
      while(line)
      {
        addChild(line);
        _y += line.height;
        line.y = _y;
        line = block.createTextLine(line, stage.stageWidth);
      }
    

如此有一个问题,就是

var line:TextLine = block.createTextLine(null, stage.stageWidth);第二个参数 是设定宽度的,如果宽度不够容下GraphicElement,这个
GraphicElement就不知道跑哪里去了。最后还是用的TextElement.只是加了个EventDispatcher,像这样:    
  var  _dipatcher:EventDispatcher=new EventDispatcher();
   
   var e1:TextElement = new TextElement('Consider, what makes a text line a ', new ElementFormat(new FontDescription(), 24));
        var e2:TextElement = new TextElement('Consider, what ', new ElementFormat(new FontDescription(), 24),_dipatcher);
      var e3:TextElement = new TextElement('?', new ElementFormat(new FontDescription(), 24));
      var e:Vector.<ContentElement> = new Vector.<ContentElement>();
      e.push(e1,e2 ,e3);
       var block:TextBlock = new TextBlock(new GroupElement(e));
      var line:TextLine = block.createTextLine(null, stage.stageWidth);
      var _y:int=10;
        var regions:Vector.<TextLineMirrorRegion>;
            var region:TextLineMirrorRegion;
            var bounds:Rectangle;
            while(line)
            {
                addChild(line);
                _y += line.ascent;
                line.y = _y;
                _y += line.ascent;
                
                if(line.mirrorRegions){
                regions = line.mirrorRegions;
                if(regions){
                    for(var j:int=0;j<regions.length;j++){
                        region=regions[j]
                        bounds = region.bounds;
                        var sp:Sprite=new Sprite();
                        sp.graphics.lineStyle(1);
                        sp.graphics.moveTo(bounds.x,bounds.y + line.y+bounds.height);
                        sp.graphics.lineTo(bounds.x+bounds.width,bounds.y + line.y+bounds.height);
                        addChild(sp);
                    }
                }
                }
                line = block.createTextLine(line,120);
            }

还可以对_dipatcher加一些鼠标事件。就可以进行其他处理了。

原文地址:https://www.cnblogs.com/sevenmoons/p/2686482.html