Skip to main content
Known Participant
November 13, 2008
Question

Exact length of a text in pixel

  • November 13, 2008
  • 5 replies
  • 742 views
Hi,

I am scripting a generator for buttons and it would be really nice if it could generate them dynamicly depending on the width of the text on the button. Therefor I would need the absolute and exact width of the particular text element - and now I wonder if there is any way to get that value out in a PS-script or if such an function is not implemented yet.

greetings, blubber
This topic has been closed for replies.

5 replies

_blubber_Author
Known Participant
November 14, 2008
I have to thank you - it seems to work perfectly, it's just that what I was looking for.

Regards
_blubber_Author
Known Participant
November 14, 2008
Paul and George, am I right that your suggestion will only work properly if I don't convert the text to paragraphtext? Because in case I convert it it seems that PSCS3 enlarges the textbox for about 20px - this value isn't always the same.
Larry, I think this example doesn't do what I want - if I understood it it sets a textbox to the desired width, but you still have to indicate it.

Thanks for your answers, I'm going to try it.

Regards
Participating Frequently
November 13, 2008
Take a look at this. You have to change the text to paragraph to read it's
dimensions.

Change app.preferences.typeUnits = TypeUnits.POINTS; to pixels

//Now change the text layer type to paragraph so you can determine the width
of the text
newTextLayer.textItem.kind = TextType.PARAGRAPHTEXT;


http://www.ps-scripts.com/bb/viewtopic.php?t=34

Larry
Known Participant
November 13, 2008
Paul is correct, a bit more specifically:

var Bnds = newLayer.bounds;
var Width = Bnds[2].value - Bnds[0].value;

I have seen cases where pixel widths of text layers gives invalid results in different versions of Photoshop, however I believe CS3 and CS4 work this way fine. At any rate I don't recall any cases where it didn't work.

Regards

George Smith
Paul Riggott
Inspiring
November 13, 2008
You could get the Layer Bounds of your text.
LB = app.activeDocument.activeLayer.bounds;