免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1686 | 回复: 0
打印 上一主题 下一主题

FLEX的字符串对齐问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:54 |只看该作者 |倒序浏览
问题
       通过连接字符串生成按列对齐的行,可以使用两种方法
一、利用s:Label组件中的属性maxDisplayedLines
       maxDisplayedLines表示显示最大的字符串长度,如果超过了该长度,自动截段字符串并添加...
       例如:<s:label width="100" maxDisplayedLines="90"/>

二、利用measureText方法
       Flex的大部分组件提供了measureText()方法,我们可以用此方法来计算字符串的实际需要的像素也就是字符串的宽度。
       这种方法的优点:宽度精度
                        缺点:需要循环计算控制
具体代码如下
  1. <s:ArrayCollection id="arr_width">
  2.             <fx:int>80</fx:int>
  3.             <fx:int>120</fx:int>
  4.             <fx:int>180</fx:int>
  5.             <fx:int>180</fx:int>
  6.         </s:ArrayCollection>
  7.         
  8.         <s:ArrayCollection id="arr_tableHeaderName">
  9.             <fx:String> 学院</fx:String>
  10.             <fx:String> 活动时间</fx:String>
  11.             <fx:String>活动名称</fx:String>
  12.             <fx:String>活动地点</fx:String>

  13.         </s:ArrayCollection>
  14. public function getRowLabel(data:ArrayCollection,widthList:ArrayCollection):String{
  15.                 if(data.length!=widthList.length)
  16.                     return "";
  17.                 
  18.                 var totalWidth:Number=0;//各列的总宽度
  19.                 var result:String=" ";//生成的字符串
  20.                 
  21.                 for(var i:int=0;i<data.length;i++){
  22.                     var label:String=data.getItemAt(i) as String;
  23.                     var width:int =widthList.getItemAt(i) as int;
  24.                     totalWidth=totalWidth+width;
  25.                     
  26.                     var len:int=label.replace(/[^\x00-\xff]/g,"xx").length;
  27.                     result=result+label;
  28.                     var lineMetrics:TextLineMetrics = measureText(result);
  29.                     var textWidth:Number = lineMetrics.width;
  30.                     
  31.                     
  32.                     if(textWidth>totalWidth){
  33.                         while(textWidth+32>totalWidth){
  34.                             result=result.substr(0,result.length-1);
  35.                             lineMetrics = measureText(result);
  36.                             textWidth = lineMetrics.width;
  37.                         }
  38.                         result=result+"....";
  39.                     }
  40.                     
  41.                     while(textWidth<totalWidth)
  42.                     {
  43.                         result=result+" ";
  44.                         lineMetrics = measureText(result);
  45.                         textWidth = lineMetrics.width;
  46.                     }
  47.                     
  48.                 }
  49.                 return result;
  50.             }

三、利用字符串所占的字节数长度来计算显示的宽度
       有人提出利用
                var len:int=label.replace(/[^\x00-\xff]/g,"xx").length;
       但是,英文和汉字一起显示的时候,无法处理超过长度时,显示“....”
       这个方法行不通


参考文献
1.http://docs.huihoo.com/flex/4/spark/components/Label.html
2.Flex组件的一些怪问题. http://flex4jiaocheng.com/blog/122
3.字符串所占实际宽度. http://blog.163.com/zlx_p/blog/static/9775498201141210347745/
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP