java - How to cut text by length in pixels? -



java - How to cut text by length in pixels? -

i have algorithmic problem: there 4 strings of text should placed in 1 line divided separator (i.e. |). width of whole line known(i.e. 500px). each string contains words of different length. want place first n-words (n depends length of each word):

one four five sixty nine one hundred sixteen

i 4 strings approximately equals length. , if 1 of strings smaller other must longer. i.e:

one | 4 five... | 60 9 | 1 hundred.

the number of initial string can less. in case number of words each string must bigger. i.e:

four 5 six one hundred sixteen

will be:

four 5 6 | 1 hundred sixteen.

is possible css or should utilize affinetransform java back-end? tried utilize approach takes lot of resources , time.

go through each character , check if can added, widthof(char) returns width of character you'll have set width each character , store them.

stringbuilder final = ""; int maxwidthpixel = 500; int currentwidthpixel = 0; for(int = 0; < text.length; i++) { char c = text.charat(i); currentwidthpixel += widthof(c); if(currentwidthpixel <= maxwidthpixel) { final.append(c); } else { break; } } homecoming final.tostring();

edit: java code.

java css

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -