AS3中将多行文字按表格格式进行排版
att:Array = ["生命","最大魔攻","物防"]
value:Array = [100020,24000,9000]
需要将上面的两个数组在界面上进行显示,要求 属性显示在上面一行, 数值显示在下面一行,然后属性和数字需要对其,属性之间需要平均分布
解决办法,通关统一长度来控制,中文的长度取2,不足的补“ ”。
1 package 2 { 3 import flash.display.Sprite; 4 import flash.utils.ByteArray; 5 6 public class StringTable extends Sprite 7 { 8 public function StringTable() 9 { 10 var att:Array = ["生命","最大魔攻","物防"] 11 var value:Array = [10020,24000,9000] 12 var str1:String = ""; 13 var str2:String = ""; 14 var str:String = "恭喜您,挑战成功"; 15 for(var i:int =0;i<att.length;i++){ 16 var tempstr1:String = att[i]; 17 var len:int=-1; 18 len = getStringLength(tempstr1); 19 while(len<10){ 20 tempstr1 += " "; 21 len++; 22 } 23 var tempstr2:String = "+"+value[i]; 24 len = getStringLength(tempstr2); 25 while(tempstr2.length<10){ 26 tempstr2 += " "; 27 len++; 28 } 29 str1 += tempstr1; 30 str2 += tempstr2; 31 } 32 str += "\n" + str1 + "\n" + str2; 33 trace(str); 34 } 35 36 /** 37 *获取字符串的占位长度,中文占2个长度 38 * @param thisString 39 * @return 40 * 41 */ 42 public function getStringLength(thisString : String) : Number{ 43 var thisStringBytsLength :ByteArray= new ByteArray(); 44 thisStringBytsLength.writeMultiByte(thisString,"gb2312"); 45 return thisStringBytsLength.length; 46 } 47 48 } 49 }