Truncating long words

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
istech
Posts: 124
Joined: Thu Sep 19, 2013 10:08 am

Truncating long words

Post by istech » Tue Jun 12, 2018 11:08 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5330
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Truncating long words

Post by dunbarx » Wed Jun 13, 2018 4:00 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2443
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Truncating long words

Post by richmond62 » Wed Jun 13, 2018 6:47 am

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.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2443
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Truncating long words

Post by richmond62 » Wed Jun 13, 2018 7:03 am

"Baby work"
-
Trunk.png
-
Trunk.livecode.zip
Here's the stack.
(6.87 KiB) Not downloaded yet

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2443
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Truncating long words

Post by richmond62 » Wed Jun 13, 2018 7:27 am

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 1 time

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

istech
Posts: 124
Joined: Thu Sep 19, 2013 10:08 am

Re: Truncating long words

Post by istech » Wed Jun 13, 2018 11:22 am

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
Attachments
trunckexample.png
trunckexample.png (6.1 KiB) Viewed 240 times

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4393
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Truncating long words

Post by jacque » Wed Jun 13, 2018 8:48 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2443
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Truncating long words

Post by richmond62 » Thu Jun 14, 2018 6:33 am

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.

jiml
Posts: 150
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Truncating long words

Post by jiml » 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

istech
Posts: 124
Joined: Thu Sep 19, 2013 10:08 am

Re: Truncating long words

Post by istech » Sat Jun 16, 2018 9:45 am

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
Attachments
performance.png
performance.png (7.65 KiB) Viewed 54 times

Post Reply

Return to “Talking LiveCode”