PDA

View Full Version : get width of the text in a combo box?


Billy T
08-18-2003, 07:15 AM
Is there any way to do it?

for example if my combo box has these items

blah
test
hello how are you?
another

then can you get the max width of any item so you can use that value to set the size of the combo box?

seems like an obvious feature but I can't find it

Thanks

freddycodes
08-19-2003, 06:45 PM
Untested, no MX here, but off the top of my head somehting like


var maxlength = 0;
for(var i=0;i<combo.getLength();i++)
{
var foo = combo.getItemAt(i);
var w = foo.label.textWidth;
maxlength = (w > maxlength) ? w : maxlength;
}
combo.setWidth(maxlength);

Billy T
08-19-2003, 08:26 PM
hmmm that didn't work but you've got me thinking that it shoudnt be too hard

getItemAt returns an object with 2 properties - label and data. As far as I know there is no reference to a movieclip or text box so we'll need another way to target each item in the combo box I think...

I shall persist

Thanks freddy

Billy T
08-19-2003, 09:32 PM
OK its official...I have no freakin idea how to target the all text box in a combo box

I got as far as

superHolder.container_mc.boundingBox_mc.fLabel_mc. labelField

...

then I tried another avenue...in the combo comp I found

for (var i=0; i<this.labels.length; i++) {
this.addItem(this.labels[i], this.data[i]);
}

and in the dataprovider comp I found

DataProviderClass.prototype.addItemAt = function(index, value)
{
if (index<this.getLength()) {
this.items.splice(index, 0, "tmp");
}
this.items[index] = new Object();
if (typeof(value)=="object") {
this.items[index] = value;
} else {
this.items[index].label = value;
}
this.items[index].__ID__ = this.uniqueID++;
var eventObj = {event:"addRows", firstRow:index, lastRow:index};
this.updateViews(eventObj);
}

but I can't find where any of this actually grabs the label clip out of the library and makes multiple copies on the stage!

Anyone know?

Thanks

freddycodes
08-19-2003, 11:47 PM
Its in the FLable class somewhere.

Anyways this is such a hack I am embarassed to put it up. But here goes anyways.


myCombo.addItem("I am");
myCombo.addItem("I am a little");
myCombo.addItem("I am a little longer");
myCombo.addItem("I am a little longer and still");
myCombo.addItem("I am a little longer");
myCombo.addItem("I am");
myCombo.addItem("I am");
function setComboWidth(what)
{
var foo = 0;
var idx = 0;
for(var i=0;i<what.getLength();i++)
{
if(what.getItemAt(i).label.length > foo)
{
foo = what.getItemAt(i).label.length;
idx = i;
}
}
trace (idx);
trace (foo);
_root.createTextField("myText", 10000, -1000,-1000,1,1);
_root.myText.autoSize = true;
_root.myText.text = what.getItemAt(idx).label;
trace (_root.myText.textWidth + 10);
what.setSize(_root.myText.textWidth + 30);
_root.myText.removeMovieClip();
}
setComboWidth(myCombo);

Billy T
08-20-2003, 12:12 AM
a hack it may be but it works like a charm!!

well done and thanks freddy

freddycodes
08-20-2003, 12:53 AM
No problem, I would like to figure out a better way though.

Billy T
08-20-2003, 01:11 AM
yeah I'm sure you could work out the path to the text boxes but I was getting dizzy going back and forth between all the comps that were sub classes of other comps that had shortcut names to different instances etc...

Let me know if you find a better way but until then...

GO THE HACK!!

freddycodes
08-20-2003, 01:22 AM
Hey your onlinegalleries app is looking pretty sweet,.

Billy T
08-20-2003, 01:26 AM
thanks man - couldn't have done it without you!

if you have any artist friends that want a site (with the cms etc) I would only charge them the cost of the hosting

just let me know

cheers

freddycodes
08-20-2003, 01:51 AM
No way man, you are the man with the plan,

I am just the answer guy.

Billy T
08-20-2003, 02:37 AM
ok then - I couldn't have done it without your answers :)

megamulli
07-24-2006, 09:42 PM
I took Freddycodes hack and customized it. Instead of creating a TextField to measure the width, I used the "depreciated" TextFormat.getTextExtent method. I like using getTextExtent when measuring text because there is no TextField left over. I've spent hours of research on Macromedia's recommended technique of measuring text width but found nothing.

ComboBox.prototype.maxLength = function():Number
{
var currLength:Number = 0;
var maxLength:Number = this.width;
var maxID:Number = 0;
var metrics:Object;
var fmt:TextFormat = new TextFormat();
for(var i = 0; i < this.getLength(); i++)
{
metrics = fmt.getTextExtent(this.getItemAt(i).label)
currLength = metrics.textFieldWidth;
if(currLength > maxLength)
{
maxLength = currLength + 12;
maxID = i;
}
}
return maxLength;
}

Then later on, to access it...

cmbBox.setSize(this[cmbBox._name].maxLength(), 22);