• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change the text Size based on amount of content

Explorer ,
Apr 28, 2021 Apr 28, 2021

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.

TOPICS
JavaScript , PDF forms

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 28, 2021 Apr 28, 2021

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).

Votes

Translate

Translate
Community Expert ,
Apr 28, 2021 Apr 28, 2021

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2021 Apr 29, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

LATEST

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2021 Apr 28, 2021

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

Limit of 7 characters in not checked in the "Options" tab.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2021 Apr 28, 2021

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines