Wednesday, 3 April 2019

Justified text

One missing thing in livecode is the justified text. However there are some workaround.
The first option is to use the free hhtextEdit widget for livecode:
It's based on the browser widget, full of option. You can download from this link: http://livecodeshare.runrev.com/stack/860/HHTextEditBasic

Otherwise you can use this code in a field:

local finaltext

on closefield
   justifyme
end closefield

on justifyme
   lock screen
   put empty into finaltext
   stripspaces
   set the dontwrap of me to true
   put the text of me into temp
   set the text of me to empty
   replace return with (space & chartonum(1) & space ) in temp #we separete words with newlines attached, and much more, newline is a word now
   put the width of me into tw
   put the number of words of temp into nw
   repeat with i=1 to nw
      if word i of temp is chartonum(1) then
         put return after me
         put the text of me after finaltext
         put empty into me
         next repeat
      end if
      put word i of temp after me
      if the formattedwidth of me > tw then
         delete the last word of me
         addspaces 0
         put word i of temp & space into me         
      else
         put space after me
      end if
   end repeat
   put the text of me after finaltext
   set the text of me to finaltext
   set the dontwrap of me to false
   unlock screen
end justifyme

on addspaces temp
   if the number of words of me is 1 then
      put the text of me after finaltext
      set the text of me to empty
      exit addspaces
   end if
   
   put the width of me into tw
   put the number of words of me -1 into nspaces #the correct number of spaces between words is nspaces -1, but we'll use mod
   put temp mod nspaces into temp
   add 1 to temp
   put space after word temp of me
   if the formattedwidth of me > tw then
      put the number of chars of word 1 to temp of me into tc
      delete char (tc +1) of me
      put the text of me & space after finaltext
      set the text of me to empty
   else
      addspaces temp
   end if
end addspaces

on stripspaces
   put the text of me into temp
   repeat for each char tchar in temp
      if (tchar is space) and (the last char of t2 is space) then
         next repeat
      else
         put tchar after t2
      end if
   end repeat
   set the text of me to t2
end stripspaces

on resizeControl
   justifyme   
end resizeControl


The result is good:

No comments:

Post a Comment