Subscribe Bookmark RSS Feed

How can the the pixel length of a string be determined?

joberski

Community Trekker

Joined:

Sep 9, 2015

I want to change the width of a list box based upon the length of the strings displayed in it, but I don't know how to determine the string lengths in pixels. I assume it is related to the font and font size being used, but I don't know how to determine that either. Any suggestions would be appreciated.

7 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

Only a partial answer, but the list box will respond to the following messages:

    Set Font  [Font] [Scripting Only]

    Set Font Name  [Action] [Scripting Only] (Font Name)

    Set Font Style  [Character] [Scripting Only]

    Set Font Size  [Action] [Scripting Only] (7|9|10|12|14|18|24|...)

    Get Font  [Action] [Scripting Only]

    Get Font Name  [Action] [Scripting Only]

    Get Font Style  [Action] [Scripting Only]

    Get Font Size  [Action] [Scripting Only]


joberski

Community Trekker

Joined:

Sep 9, 2015

Thanks Ian, that helps. Is there a difference between font and font name?

Craige_Hales

Staff

Joined:

Mar 21, 2013

I use the scripting index to figure out how the messages work: help->scripting index.  search for "set font".  9782_setFont.PNG

9783_setFontName.PNG

Craige
joberski

Community Trekker

Joined:

Sep 9, 2015

Ah… Now I see. Thanks.

kevin_c_anderso

Community Trekker

Joined:

Jun 5, 2014

...and, wouldn't it be cool if there was an unambiguous answer to your original question?

Unfortunately, while the Font Size in Points is now pretty standard (1/72 in = 1 point), the pixel length of a string is determined by the OS type and display resolution on which the string is viewed.  You can get less ambiguous by restricting your specification of the OS and display resolution.

joberski

Community Trekker

Joined:

Sep 9, 2015

So, it appears that is possible, but not easy.

Since the script I’m writing is only for internal use, I think I’ll just stick with my current solution of putting the list box inside of a scroll box, and sizing the list box wider than the longest string I’m likely to encounter.

Thanks to everyone for your helpful replies.

Here’s the line of code in case anyone is interested.

ScrollBox(Size(10, 200), flexible(1), fileLb2 = ListBox({}, width(1500), nlines(100)))

Craige_Hales

Staff

Joined:

Mar 21, 2013

You can get JMP to tell you the size of a text box that holds a string after the string has been displayed. 


New Window( "x", t = Text Box( "" ) );


strings = {"a", "aa", "aaa", "i", "ii", "iii", "aaaiii", "the quick brown fox"};


For( i = 1, i <= N Items( strings ), i++,


  t << settext( strings );


  t << reshow;


  Write( strings, t << getsize, "\!n" );


);


a{10, 24}

aa{19, 24}

aaa{28, 24}

i{6, 24}

ii{10, 24}

iii{14, 24}

aaaiii{41, 24}

the quick brown fox{154, 24}

Craige