mercoledì 3 aprile 2019

Testo giustificato

Per fare un testo giustificato in livecode non c'è l'opzione già pronta,comunque ci sono delle soluzioni.
Una è un widget gratuito per testi, hhtextEdit:
E' basato sul widget browser ed è pieno di opzioni. Potete scaricarlo da: http://livecodeshare.runrev.com/stack/860/HHTextEditBasic

In alternativa, come seconda soluzione, potete mettere questo codice in un campo di testo:

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


Il risultato è buono: