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.
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
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.
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
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.
-
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
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.
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
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
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
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.
jiml wrote: Fri Jun 15, 2018 12:28 amtruncateToFit(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.