How to use superscripting in flex?

2019-01-27 06:38发布

问题:

I want to add a label in flex to display m/s2 (read meters per second square). I would need to use superscripting for this.

I have tried out the following code which is giving me a compilation error.

var richtxt1:RichText = new RichText();
richtxt1.text="m/s";
var richtxt2:RichText = new RichText();
var span:SpanElement = new SpanElement();
span.text = "2";    
span.baselineShift = "superscript"; 
richtxt2.addChild(span);
richtxt1.text=rixhtxt1.txt + richtxt2.text

I am getting a compilation error for the line richtxt2.addChild(span)

The error is

Implicit coercion of a value of type flashX.textLayout.elements.SpanElement 
to unrelated type flash.Display.DisplayObject

回答1:

I think you've to do something like this

var xmlText:String = "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008'>" +
                     "m/s <span baselineShift='superscript'>2</span>" +
                     "</TextFlow>";

var txtFlow:TextFlow = TextFlowUtil.importFromXML(xmlText);

var richTxt:RichText = new RichText();
richtxt.textFlow = txtFlow;

I've not tested it so please excuse me of any compilation errors.



回答2:

This is the code I used in my iPad app to accomplish the above:

var xmlText:String = "m/s <span baselineShift='superscript'>2</span>";
var txtFlow:TextFlow = TextFlowUtil.importFromString(xmlText);
var richTxt:RichText = new RichText();              
richTxt.textFlow = txtFlow;
this.addElement(richTxt);

It is based on kaychaks and information I found from Adobe's website. The differences are

  • I took out the TextFlow markup but left in the HTML;
  • importFromString rather than importFromXML; and
  • I added this.addElement(richText) to display the element.