- 论坛徽章:
- 0
|
问题 通过连接字符串生成按列对齐的行,可以使用两种方法 一、利用s:Label组件中的属性maxDisplayedLines maxDisplayedLines表示显示最大的字符串长度,如果超过了该长度,自动截段字符串并添加... 例如:<s:label width="100" maxDisplayedLines="90"/>
二、利用measureText方法 Flex的大部分组件提供了measureText()方法,我们可以用此方法来计算字符串的实际需要的像素也就是字符串的宽度。 这种方法的优点:宽度精度 缺点:需要循环计算控制 具体代码如下
- <s:ArrayCollection id="arr_width">
-
<fx:int>80</fx:int>
-
<fx:int>120</fx:int>
-
<fx:int>180</fx:int>
-
<fx:int>180</fx:int>
-
</s:ArrayCollection>
-
-
<s:ArrayCollection id="arr_tableHeaderName">
-
<fx:String> 学院</fx:String>
-
<fx:String> 活动时间</fx:String>
-
<fx:String>活动名称</fx:String>
-
<fx:String>活动地点</fx:String>
-
-
</s:ArrayCollection>
-
public function getRowLabel(data:ArrayCollection,widthList:ArrayCollection):String{
-
if(data.length!=widthList.length)
-
return "";
-
-
var totalWidth:Number=0;//各列的总宽度
-
var result:String=" ";//生成的字符串
-
-
for(var i:int=0;i<data.length;i++){
-
var label:String=data.getItemAt(i) as String;
-
var width:int =widthList.getItemAt(i) as int;
-
totalWidth=totalWidth+width;
-
-
var len:int=label.replace(/[^\x00-\xff]/g,"xx").length;
-
result=result+label;
-
var lineMetrics:TextLineMetrics = measureText(result);
-
var textWidth:Number = lineMetrics.width;
-
-
-
if(textWidth>totalWidth){
-
while(textWidth+32>totalWidth){
-
result=result.substr(0,result.length-1);
-
lineMetrics = measureText(result);
-
textWidth = lineMetrics.width;
-
}
-
result=result+"....";
-
}
-
-
while(textWidth<totalWidth)
-
{
-
result=result+" ";
-
lineMetrics = measureText(result);
-
textWidth = lineMetrics.width;
-
}
-
-
}
-
return result;
-
}
三、利用字符串所占的字节数长度来计算显示的宽度 有人提出利用 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/
|
|