Flash ActionScript 3分发文本字段
动作脚本3 我今天一直在绞尽脑汁想最好的方法,但我最终把我认为是一项简单的任务复杂化了 我有一些文本字段排列在一起,都是固定宽度的,即([宽度]) 我想做的是能够选择要显示的细节,然后将删除的文本字段的宽度重新分配给其余部分(注意,您只能从右侧删除),因此如果我要显示为“tf3”,120(70+50)将需要均匀分布到其他框:Flash ActionScript 3分发文本字段,flash,actionscript-3,textfield,alignment,distribute,Flash,Actionscript 3,Textfield,Alignment,Distribute,动作脚本3 我今天一直在绞尽脑汁想最好的方法,但我最终把我认为是一项简单的任务复杂化了 我有一些文本字段排列在一起,都是固定宽度的,即([宽度]) 我想做的是能够选择要显示的细节,然后将删除的文本字段的宽度重新分配给其余部分(注意,您只能从右侧删除),因此如果我要显示为“tf3”,120(70+50)将需要均匀分布到其他框: [ 80 + 50 + 50 = 180 ] tf1 [80/180*120 + 80 = 133.3]
[ 80 + 50 + 50 = 180 ]
tf1
[80/180*120 + 80 = 133.3]
tf2
[50/180*120 + 50 = 83.3]
tf3
[50/180*120 + 50 = 83.3]
然后再次将其对齐(x+宽度等):
如果有更好的方法,我相信我可以用一些简单的函数来实现
我现在可能也把这个问题复杂化了,但我试试看,有人有什么想法吗?我是不是理解错了什么,或者不是简单到:
boxWidth = (availableWidth - (padding + margins)) / numberOfFieldsShown
让我澄清一下,你有N个不同宽度的文本字段,你想删除其中的一个或多个,重新排列剩余的M个文本字段,按比例重新分配它们的宽度(也就是说,如果文本字段T1是文本字段T2的两倍宽度,那么在重新排列它们之后仍然是如此) 这实际上不是一个非常简单的问题,但我在下面尝试了一下。将文本字段数组(从左到右)、要保留的数字和可选的X偏移量传递给它
<强>注:< /强>我几个月没有做AS3,所以把下面的代码看作是伪代码——你必须编辑它才能使它正常工作。
function arrangeTextFields(textFields:Array, keep:int, xOffset:Number = 0) {
// we can't keep more fields than are provided
keep = Math.min(keep, textFields.length);
var totalWidth = 0, // the total width of all textfields
keptOrigWidth = 0, // the total combined widths of the ones you're keeping
origNum:int = textFields.length;
// calculate the above values by addition in a loop
// (because AS3 lacks an inject/reduce method)
for(var i:int, i++, i < origNum) {
totalWidth += textFields[i].width;
if(i < take) {
keptOrigWidth += textFields[i].width;
}
}
var ratio:Number = totalWidth / takenOrigWidth, // the ratio we'll resize each field by to keep them proportional
currentX:int = 0;
textFields = textFields.slice(0, keep - 1); // throw away the fields we're not keeping
for(int i = 0; i < take; i++) {
textFields[i].width = textFields[i].width * ratio; // resize it
textFields[i].x = currentX + xOffset; // move it
currentX += textFields[i].width;
}
}
函数arrangeTextFields(textFields:Array,keep:int,xOffset:Number=0){
//我们不能保留超过所提供数量的字段
keep=Math.min(keep,textFields.length);
var totalWidth=0,//所有文本字段的总宽度
keptOrigWidth=0,//保留的宽度的总和
origNum:int=textFields.length;
//通过循环中的加法计算上述值
//(因为AS3缺少注入/减少方法)
for(变量i:int,i++,i
注意:此代码实际上不会从视图中删除“unkept”文本字段;你必须加上这个,或者在别的地方做
boxWidth = (availableWidth - (padding + margins)) / numberOfFieldsShown
function arrangeTextFields(textFields:Array, keep:int, xOffset:Number = 0) {
// we can't keep more fields than are provided
keep = Math.min(keep, textFields.length);
var totalWidth = 0, // the total width of all textfields
keptOrigWidth = 0, // the total combined widths of the ones you're keeping
origNum:int = textFields.length;
// calculate the above values by addition in a loop
// (because AS3 lacks an inject/reduce method)
for(var i:int, i++, i < origNum) {
totalWidth += textFields[i].width;
if(i < take) {
keptOrigWidth += textFields[i].width;
}
}
var ratio:Number = totalWidth / takenOrigWidth, // the ratio we'll resize each field by to keep them proportional
currentX:int = 0;
textFields = textFields.slice(0, keep - 1); // throw away the fields we're not keeping
for(int i = 0; i < take; i++) {
textFields[i].width = textFields[i].width * ratio; // resize it
textFields[i].x = currentX + xOffset; // move it
currentX += textFields[i].width;
}
}