cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
pmroz
Super User

Automatically set nlines for text edit box?

Hello fellow JMPers,

With text boxes you set the wrap and width pixels and the height of the textbox is automatically set large enough to accommodate the text.  For example:

my_narr = "Bacon ipsum dolor amet sirloin short loin meatloaf doner, shoulder swine brisket bacon pig boudin alcatra ham hock chuck hamburger corned beef. Pork chop landjaeger sausage frankfurter biltong doner pork loin ham pastrami fatback sirloin. Brisket prosciutto corned beef short ribs. Ball tip filet mignon shoulder, landjaeger corned beef fatback cow shank venison pork chop bacon meatball bresaola leberkas. Tri-tip short ribs andouille tail prosciutto doner swine ground round meatloaf pancetta boudin.

Brisket beef bacon, landjaeger beef ribs tenderloin alcatra t-bone short ribs venison fatback capicola picanha pig. Salami brisket doner drumstick pork loin shank chicken corned beef short ribs chuck meatball t-bone. Andouille frankfurter rump strip steak landjaeger bacon beef ham hock hamburger flank picanha. Brisket short loin flank hamburger pork belly sausage bresaola doner. Biltong prosciutto jerky, spare ribs porchetta picanha rump pork swine pork loin cupim.";

nw = new window("Test",

      tb = text box(my_narr),

);

tb << set wrap(600) << set width(600);

I have some long texts that I put into text edit boxes, because users want to copy/paste portions of the information.  However text edit boxes need to set the height using set nlines


nw = new window("Test",

      teb = text edit box(my_narr),

);

teb << set wrap(600) << set width(600) << set nlines(10);


Is there some slick way to calculate the number of lines given a lengthy text?  I came up with a way to do it, but it seems clunky.  I've attached the code necessary to run this example.  There are two functions, get_num_lines and my_parse.  I had to write my own parser to handle arbitrarily long delimiters, and two or more occurrences of a delimiter with nothing in between.

nw = new window("Test",

      teb = text edit box(my_narr),

);

num_lines = get_num_lines(my_narr, 600);

teb << set wrap(600) << set width(600) << set nlines(num_lines);

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Automatically set nlines for text edit box?

One idea would be to utilize the automatic sizing capability of the text box. The number of lines appears to equal this ratio: height_of_textbox / (fontsize+1).

my_narr =

"Bacon ipsum dolor amet sirloin short loin meatloaf doner, shoulder swine brisket bacon pig boudin alcatra ham hock chuck hamburger corned beef. Pork chop landjaeger sausage frankfurter biltong doner pork loin ham pastrami fatback sirloin. Brisket prosciutto corned beef short ribs. Ball tip filet mignon shoulder, landjaeger corned beef fatback cow shank venison pork chop bacon meatball bresaola leberkas. Tri-tip short ribs andouille tail prosciutto doner swine ground round meatloaf pancetta boudin.

Brisket beef bacon, landjaeger beef ribs tenderloin alcatra t-bone short ribs venison fatback capicola picanha pig. Salami brisket doner drumstick pork loin shank chicken corned beef short ribs chuck meatball t-bone. Andouille frankfurter rump strip steak landjaeger bacon beef ham hock hamburger flank picanha. Brisket short loin flank hamburger pork belly sausage bresaola doner. Biltong prosciutto jerky, spare ribs porchetta picanha rump pork swine pork loin cupim.";

w = 450; // Wrap and width

tb = Text Box(my_narr, set wrap(w), set width(w), hide(1)); // Temporary invisible textbox

nw = New Window("Test", tb);

// Get size info

height = (tb << get size)[2];

fontsize = tb << get font size;

nlines = height / (fontsize + 1);

//nlines++; // Add +1 to get some padding; selection of TextEditBox content increases its size

tb << delete;

//Append TextEditBox with same size as the TextBox

teb = Text Edit Box(my_narr, set wrap(w), set width(w), set nlines(nlines));

nw << append(teb);

View solution in original post

2 REPLIES 2
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Automatically set nlines for text edit box?

One idea would be to utilize the automatic sizing capability of the text box. The number of lines appears to equal this ratio: height_of_textbox / (fontsize+1).

my_narr =

"Bacon ipsum dolor amet sirloin short loin meatloaf doner, shoulder swine brisket bacon pig boudin alcatra ham hock chuck hamburger corned beef. Pork chop landjaeger sausage frankfurter biltong doner pork loin ham pastrami fatback sirloin. Brisket prosciutto corned beef short ribs. Ball tip filet mignon shoulder, landjaeger corned beef fatback cow shank venison pork chop bacon meatball bresaola leberkas. Tri-tip short ribs andouille tail prosciutto doner swine ground round meatloaf pancetta boudin.

Brisket beef bacon, landjaeger beef ribs tenderloin alcatra t-bone short ribs venison fatback capicola picanha pig. Salami brisket doner drumstick pork loin shank chicken corned beef short ribs chuck meatball t-bone. Andouille frankfurter rump strip steak landjaeger bacon beef ham hock hamburger flank picanha. Brisket short loin flank hamburger pork belly sausage bresaola doner. Biltong prosciutto jerky, spare ribs porchetta picanha rump pork swine pork loin cupim.";

w = 450; // Wrap and width

tb = Text Box(my_narr, set wrap(w), set width(w), hide(1)); // Temporary invisible textbox

nw = New Window("Test", tb);

// Get size info

height = (tb << get size)[2];

fontsize = tb << get font size;

nlines = height / (fontsize + 1);

//nlines++; // Add +1 to get some padding; selection of TextEditBox content increases its size

tb << delete;

//Append TextEditBox with same size as the TextBox

teb = Text Edit Box(my_narr, set wrap(w), set width(w), set nlines(nlines));

nw << append(teb);

pmroz
Super User

Re: Automatically set nlines for text edit box?

Thanks MS that got me on the right track.  Thanks for a simple slick solution!

This equation seems to work a little better than height / (fontsize+1):

nlines = max(1, floor(.6264918 * height / fontsize - 1.567659));


Addendum:

This works better for low numbers of characters:


nlines = max(1, round(.5204443 * height / fontsize));