Page 1 of 1

Truncating long words

Posted: Tue Jun 12, 2018 11:08 pm
by istech
Hi Livecoders,

Got a small issue that been nagging at me. I wonder if anyone can help.

I use the function listed below from Livecode to truncate my text fields. However, if there is a long word at the end of the field the Truncating is not performed on the text.

Apparently, this method only works if there are spaces in the text. i.e If there was a field with "supercallafraggerlisticxpallydoeus" no truncating would take place. Can someone offer or hint at a way to work around this limit. I considered introducing spaces to the long words. But does not seem very intuitive. Maybe I am missing something.

Thanks for your time.

Code: Select all

function truncatedToFit pString, pWidth
   local tTruncatedLine
   
   create invisible field
   set the width of last field to pWidth
   put pString into last field
   put line 1 of the formattedText of last field into tTruncatedLine
   delete last field
   send "choose browse tool" to me in 1 millisecond
   if tTruncatedLine is not pString then put "..." after tTruncatedLine
   return tTruncatedLine
end truncatedToFit

Re: Truncating long words

Posted: Wed Jun 13, 2018 4:00 am
by dunbarx
Hi.

The "formatted" properties of fields are deeply dependent on the space delimiter for words. This has been seen as either a feature or a hassle forever.

To do what I think you are intending, you will have to either introduce spaces after the appropriate char or count and partition the chars themselves. Either method would fall nicely into your comment about not being intuitive.

But the only thing that matters is results, eh? And if you write a neat little function to do either, you will find that function very intuitive indeed.

Craig Newman

Re: Truncating long words

Posted: Wed Jun 13, 2018 6:47 am
by richmond62
You are being a bit ambiguous, as your heading reads "Truncating long words",
yet, in your text you write about truncating text fields.

I am going to try to address the problem of long words blocking the truncation of
text IN text fields, and I hope that will help you.

Re: Truncating long words

Posted: Wed Jun 13, 2018 7:03 am
by richmond62
"Baby work"
-
Trunk.png
-
Trunk.livecode.zip
Here's the stack.
(6.87 KiB) Downloaded 300 times

Code: Select all

on mouseUp
   put the number of chars of fld "tf" into CC
   if CC > 1000 then
      put fld "tf" into TEKST
      put empty into fld "tf"
      put 1 into KOUNT
      repeat until KOUNT > 1000
         put char 1 of TEKST after fld "tf"
         delete char 1 of TEKST
         add 1 to KOUNT
      end repeat
      end if
end mouseUp

Re: Truncating long words

Posted: Wed Jun 13, 2018 7:27 am
by richmond62
HOWEVER . . .

looking at your initial code I think you are trying to be far more clever than my silly stuff: you are trying to truncate your text so it fits inwith your textField window.

This should be perfectly possible with a small modification of my script.

Probably not a bad idea to play around with pageRanges. 8)
-
Trunk2.png
-
Trunk2.livecode.zip
Here's the stack.
(6.28 KiB) Downloaded 315 times

Code: Select all

on mouseUp
   put item 2 of the pageRanges of fld "tf" into PR
   put 1 into LAP 
   repeat until (char 1 of PR) = cr
      put char 1 of PR after RP
      delete char 1 of PR
      add 1 to LAP
   end repeat
   ------
      put fld "tf" into TEKST
      put empty into fld "tf"
      put 1 into KOUNT
      repeat until KOUNT > PR
         put char 1 of TEKST after fld "tf"
         delete char 1 of TEKST
         add 1 to KOUNT
      end repeat
end mouseUp

Re: Truncating long words

Posted: Wed Jun 13, 2018 11:22 am
by istech
Hi Richard,

I have attached a picture of the current issue. I have also attached two examples I am testing out. Similar to yours. See below. I suppose either option will work. Was looking for a property or command I did not know about that I could use. But as Craig said as long as it works. :D

Many thanks

Tony
example 1

Code: Select all

on mouseUp
   repeat until the formattedwidth of fld "SOMETEXT2" < the width of fld "SOMETEXT2" -3
      delete the last char in fld "SOMETEXT2"
   end repeat
end mouseUp
example 2

Code: Select all

on mouseup
   put fld "sometext" into sometextvar
   put empty into fld "sometext"
   repeat until the formattedwidth of fld "sometext" >= the width of fld "sometext" -3
      add 1 to tCharNum
      put char tCharNum of sometextvar after fld "sometext"
   end repeat
end mouseup

Re: Truncating long words

Posted: Wed Jun 13, 2018 8:48 pm
by jacque
Writing to a field is one of the slowest operations in LC. It will be faster if you lock the screen, do the text adjustments, and then unlock the screen.

Either of the methods work, though I prefer the first only because it's more concise and in many cases would require fewer repetitions in the repeat loop.

Re: Truncating long words

Posted: Thu Jun 14, 2018 6:33 am
by richmond62
Writing to a field is one of the slowest operations in LC.
Yes, it is: but writing into a string variable does not have the visual element that is useful for teaching.

Re: Truncating long words

Posted: Fri Jun 15, 2018 12:28 am
by jiml
truncateToFit(stringToTruncate, nameOfDestinationField)
is a function to determine where to truncate a string so that it fits into a destination field with ellipsis as needed.

stringToTruncate = Any string or expression that evaluates to a string.
nameOfDestinationField = the name of the destination field, such as "myTruncatedField".

This function uses the new measureText() function, so it does not need to repeatedly manipulate the contents of the destination field in order to calculate where to truncate.
There is no need to lock the screen as the destination field is only updated once with the truncated text that the function returns. Therefore this is fast.

The function automatically takes into account the destination field's text size and margins.

Available here: http://netrin.on-rev.com/truncater.livecode

The function is in the card script.

Enjoy!
Jim Lambert

Re: Truncating long words

Posted: Sat Jun 16, 2018 9:45 am
by istech
jiml wrote:
Fri Jun 15, 2018 12:28 am
truncateToFit(stringToTruncate, nameOfDestinationField)
is a function to determine where to truncate a string so that it fits into a destination field with ellipsis as needed.

stringToTruncate = Any string or expression that evaluates to a string.
nameOfDestinationField = the name of the destination field, such as "myTruncatedField".

This function uses the new measureText() function, so it does not need to repeatedly manipulate the contents of the destination field in order to calculate where to truncate.
There is no need to lock the screen as the destination field is only updated once with the truncated text that the function returns. Therefore this is fast.

The function automatically takes into account the destination field's text size and margins.

Available here: http://netrin.on-rev.com/truncater.livecode

The function is in the card script.

Enjoy!
Jim Lambert
Thanks a lot for this Jim this is exactly what I was looking for. Going to performance test this and see how it holds up. Many thanks again.

--update--
Performance test (with no locked screen) 10000 iterations

Re: Truncating long words

Posted: Sun Jun 14, 2020 2:09 am
by 5imon
Istech, do you still happen to have the truncater dot livecode stack or the modified one? The link appears corrupt and I cannot open it.

Re: Truncating long words

Posted: Sun Jun 14, 2020 10:29 am
by richmond62
I have just downloaded that stack and opened it successfully in LC 9.6.0 on Macintosh 10.15.

I will attach a zip file of the stack here.