Padding lines with spaces?

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
littleblackdog11
Posts: 1
Joined: Sat Oct 24, 2020 10:12 am

Padding lines with spaces?

Post by littleblackdog11 » Sat Oct 24, 2020 10:13 am

there an quick and easy way to pad lines with spaces to match the
longest line in the list?

I can picture in my mind how to do this using two repeat loops, first to
find the longest line, and second to add the spaces to all the lines that
don't equal the length of the longest line, but I am hoping there is a
easier or faster way to do this.

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

Re: Padding lines with spaces?

Post by richmond62 » Sat Oct 24, 2020 10:44 am

I suppose you could count the number of characters in a line,
BUT with most fonts letters are NOT all the same length.

Counting the letters in a line should be dead easy.

Something like this:

Code: Select all

on mouseUp
   put 1 into LYNE
   repeat until line LYNE of fld "myLIST" is empty
      put the number of chars in line LYNE of fld "myLIST" into line LYNE of fld "sentenceL"
      add 1 to LYNE
   end repeat
end mouseUp
-
Screenshot 2020-10-24 at 12.43.50.png
-
Attachments
Line lengths.livecode.zip
Here's the stack.
(1.1 KiB) Downloaded 147 times

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

Re: Padding lines with spaces?

Post by richmond62 » Sat Oct 24, 2020 12:02 pm

Thanks to the good offices of Klaus . . .

Who "reminded" me about formattedWidth
-
richtInDowp.jpeg
richtInDowp.jpeg (6.31 KiB) Viewed 2764 times
-

You can try this sort of thing:

Code: Select all

on mouseUp
   put 1 into LYNE
   repeat until line LYNE of fld "myLIST" is empty
      put the formattedWidth of line LYNE of fld "myLIST" into line LYNE of fld "sL2"
      add 1 to LYNE
   end repeat
end mouseUp
And, obviously, append spaces until your formattedWidth is the one you require of each line, a bit like this:

Code: Select all

put the formatted width of fld "fSENTENCE" into XXX
repeat until XXX > 500
put " " after fld "fSENTENCE"
put the formatted width of fld "fSENTENCE" into XXX
end repeat

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

Re: Padding lines with spaces?

Post by dunbarx » Sun Oct 25, 2020 1:28 am

Hi.

The formattedWidth of a field is the width, in pixels, required to display the longest line assuming the dontWrap property is "true". So if you have a field with two lines,"12345 67890" on line 1 and "123" on line 2, the formatted width will give you the width you need to display line 1. Line 2 has "space" to the right.

Note the spaces between the characters in line 1.

When you say "pad with spaces", what do you mean? In the above example, the textAlign property lets you place the contents of those two lines in three different ways. Beyond that, what does "padding" do for you?

Craig
Last edited by dunbarx on Sun Oct 25, 2020 1:30 am, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9841
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Padding lines with spaces?

Post by FourthWorld » Sun Oct 25, 2020 1:28 am

There are several ways to do what you're asking for, and the ones shared so far are pretty good. But may I ask why you're doing this? If it's for alignment there may be much better options.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Talking LiveCode”