Copy link to clipboard
Copied
Hi, I have selected "Auto" in text size of a text field. Now, I want to set its size to 10 pt. If the text field gets filled, the text size should reduce to 8 pt. I first tried to implment it based on length of text (characters), code is given below.
On Focus,
event.target.textSize = 0;
On Blur,
var len = event.target.value.toString().length;
if (len < 7) {
event.target.textSize = 10;
}
else {
event.target.textSize = 0;
}
but issue with this strategy is that different characters have different width that makes it difficult to implement it using above code, as the content of different text boxes looks with different random font sizes.
I want to know if there is a way to implement it using "event.fieldFull" techniue or any other overflow detection. If text is not overflown, text size should be 10, if it overflows, it should be 8 pt.
Copy link to clipboard
Copied
No, fieldFull won't work for that purpose. The only way is if you write a function that calculates the width of the text based on the characters in it (which will vary per font used, of course).
Copy link to clipboard
Copied
No, fieldFull won't work for that purpose. The only way is if you write a function that calculates the width of the text based on the characters in it (which will vary per font used, of course).
Copy link to clipboard
Copied
I don't think its possible in Adobe Acrobat JavaScript to calculate width of text in a field.
I have seen some codes for calculating width and height of text for web based forms.
Copy link to clipboard
Copied
It's not impossible, but it is very complicated. By the way, one easy way of getting around it is to use a monospace font, and then all characters have the same width...
Copy link to clipboard
Copied
Just curious, why does the text would overflow if you set a limit to 7 characters long in that field?
Copy link to clipboard
Copied
Limit of 7 characters in not checked in the "Options" tab.
Copy link to clipboard
Copied
++ Adding to the guidance of try67,
These two lines are doing the job on my end as a Custom Format Script:
if ( (event.fieldFull = true) && (event.value.toString().length <= 7) ) event.target.textSize = 10;
else if ( (event.fieldFull = true) && (event.value.toString().length > 7) ) event.target.textSize = 8;
It may not be a function to calculate width and more advanced textfield object properties, but it does react to what the user will type in in that field, plus it will commit to the desired font size properties as soon as you hit the Enter key (or paste a string of text from another content source).

