[SOLVED] pixels to string length conversion lazarus / 21-General ---------------------------------------------------------------------- petevick · 2023-11-21 12:23 I want to reduce the length of a string to fit in a known width panel. I know that LeftStr(s, n) takes n as the number of characters, but lets say the panel is 500px, I'd want the string result of LeftStr() to fit in 450px, so is there a conversion of pixels to string length. ? This needs to run in Windows and Linux. ---------------------------------------------------------------------- Zvoni · 2023-11-21 12:28 Maybe this? https://www.freepascal.org/docs-html/rtl/graph/textwidth.html Basically, start with the full String, and then walk backwards from the end of the String (character-wise or word-wise) until you hit your limit And remember: The result is dependant on the Font, Fontsize etc. https://lazarus- ccr.sourceforge.io/docs/lcl/graphics/tcanvas.textwidth.html ---------------------------------------------------------------------- Handoko · 2023-11-21 12:33 Quote from: petevick on November 21, 2023, 01:23:34 pm... so is there a conversion of pixels to string length. ? No. But string to pixels length is available. Pixels to string length is not possible because no one know what characters are in the string. The width of letter "W" is not the same as the width of letter "i". You can use TextWidth as mentioned by Zvoni or alternatively you can use TextExtend. The demo code of TextExtend can be found here: https://forum.lazarus.freepascal.org/index.php/topic,46775.msg333936.h tml#msg333936 ---------------------------------------------------------------------- petevick · 2023-11-21 12:34 Quote from: Zvoni on November 21, 2023, 01:28:50 pmMaybe this? https://www.freepascal.org/docs-html/rtl/graph/textwidth.html Basically, start with the full String, and then walk backwards from the end of the String (character-wise or word-wise) until you hit your limit And remember: The result is dependant on the Font, Fontsize etc. thanks Zvoni, that looks like it'll do what I'm after ---------------------------------------------------------------------- petevick · 2023-11-21 12:37 Quote from: Handoko on November 21, 2023, 01:33:10 pmQuote from: petevick on November 21, 2023, 01:23:34 pm... so is there a conversion of pixels to string length. ? No. But string to pixels length is available. You can use TextWidth as mentioned by Zvoni or alternatively you can use TextExtend. The demo code of TextExtend can be found here: https://forum.lazarus.freepascal.org/index.php/topic,46775.msg333936.h tml#msg333936 thanks Handoko, I'll take a look at that as well ---------------------------------------------------------------------- petevick · 2023-11-21 14:46 Thanks for the replies, the following is what I've finished up with. Panel in the code refers to a TStatusBar Panel. Works in Windows and Linux. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i          if TextWidth(Panel.Text)> 600 then         begin           n:= Length(Panel.Text)-1;           repeat             Panel.Text:= LeftStr(Panel.Text, n);             n:= n - 1;           until TextWidth(Panel.Text) < 575;           Panel.Text:= Panel.Text + '...';         end;