给文本加上标-使用Adobe Labs发布的文本布局框架Text Layout Framework (TLF)
1. 以A的3次方为例,我们输入以下代码:
2.
3. /**
4. * 部分代码参考Adobe文档:
5. * http://help.adobe.com/zh_CN/AS3LCR/Flash_10.0/flash/text/engine/package-detail.html
6. * by kingnare.com
7. */
8.
9. package
10. {
11. import __AS3__.vec.Vector;
12.
13. import flash.display.Sprite;
14. import flash.text.engine.ContentElement;
15. import flash.text.engine.ElementFormat;
16. import flash.text.engine.FontDescription;
17. import flash.text.engine.FontWeight;
18. import flash.text.engine.GroupElement;
19. import flash.text.engine.TextBaseline;
20. import flash.text.engine.TextBlock;
21. import flash.text.engine.TextElement;
22. import flash.text.engine.TextLine;
23.
24. [SWF(width="800", height="600", backgroundColor="#FFFFFF", framerate="24")]
25.
26. public class TextEngineTest_TextBaseline extends Sprite
27. {
28. public function TextEngineTest_TextBaseline()
29. {
30. //字体属性
31. var fd1:FontDescription = new FontDescription("Arial");
32. //格式设置
33. var ef1:ElementFormat = new ElementFormat(fd1, 16);
34.
35. var fd2:FontDescription = new FontDescription("Arial");
36. var ef2:ElementFormat = new ElementFormat(fd2, 10);
37. //将基线上移8个像素
38. ef2.baselineShift = -8;
39.
40. //创建已设置格式的文本的字符串
41. var te1:TextElement = new TextElement("A",ef1);
42. var te2:TextElement = new TextElement("3",ef2);
43.
44. var groupVector:Vector.<ContentElement> = new Vector.<ContentElement>();
45. groupVector.push(te1, te2);
46. //组成ContentElement集合
47. var groupElement = new GroupElement(groupVector);
48. //创建文本块
49. var textBlock:TextBlock = new TextBlock();
50. textBlock.content = groupElement;
51. //显示文本
52. createTextLines(textBlock);
53. }
54.
55. //显示文本
56. private function createTextLines(textBlock:TextBlock):void
57. {
58. var yPos = 20;
59. var line_length:Number = 450;
60. var textLine:TextLine = textBlock.createTextLine(null,line_length);
61. while (textLine)
62. {
63. addChild(textLine);
64. textLine.x = 15;
65. yPos += textLine.height + 8;
66. textLine.y=yPos;
67. textLine=textBlock.createTextLine(textLine,line_length);
68. }
69. }
70. }
71. }