Copy link to clipboard
Copied
Hi,
I'm wanting to find the width of a text box and if it's longer than say 200 pixels, decrement the font size (points) by one until the text box width is less than 200 pixels. I'm guessing I'm going to need some form of loop here but I'm having problems with this.
I've not included all of my code just the important parts.
TextItem.Size = 50
TextItem.Kind = 2 (to allow me to find the width)
Dim TextWidth = TextItem.Width
Now my thoughts for the loop are...
While TextWidth > 200
TextItem.Size -= 1
TextWidth = TextItem.Width
End While
I'm not the best programmer so don't know if that loop is completely correct but my idea behind it is that while the text box width is greater than 200, decrease the font size by one, find the new text box width then if this is still greater than 200, repeat the process until it's less.
The part i seem to be having a problem with is the "TextWidth = TextItem.Width" inside the while loop. From testing I know that when the text box width is greater than 200 pixels, it correctly enters the loop, decreases the font size, BUT I can't seem to get it to find the new width of the text box. Instead it appears to be still using the original value for width and hence never ending the loop.
Can somebody please point me in the direction of where I'm going wrong?
Thanks in advance.
That code looks like VBS( or some other VB flavor ). If VBS works the same way javascript does, changing the text size will not change the text box width.
There are two types of text layers, point text and paragraph text. The width property only applies to paragraph text but is the width of the text bounding box, not the width of the text itself.
You should be able to get the width of the text from the text layer's bounds property.
Copy link to clipboard
Copied
That code looks like VBS( or some other VB flavor ). If VBS works the same way javascript does, changing the text size will not change the text box width.
There are two types of text layers, point text and paragraph text. The width property only applies to paragraph text but is the width of the text bounding box, not the width of the text itself.
You should be able to get the width of the text from the text layer's bounds property.
Copy link to clipboard
Copied
Thanks for the reply Michael.
I just assumed that changing the font size would change the size of the text box. That's obviously why it's not exiting the loop then and hence not working.
I'll have a look into the bounds property like you suggested then, thanks.
Copy link to clipboard
Copied
If you don't mind pointing me in the right direction with this bounds property it would be much appreciated. I've tried searching through this forum, the photoshop scripting guides and google but I'm not finding the answer I'm after.
From what I've found it looks to be something like...
Dim TextWidth = ArtLayer.Bounds[2] - ArtLayer.Bounds[0]
The language I'm using is VB.
Thanks in advance.
Copy link to clipboard
Copied
Seems I've worked this out now by using the bounds property.
Thanks for your help 🙂
Copy link to clipboard
Copied
Glad you got it working. I was looking in the VBS guide to see how it's done so I could post an example but I didn't make much progress.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now