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.
Padding lines with spaces?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Padding lines with spaces?
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:
-
-
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
- Attachments
-
- Line lengths.livecode.zip
- Here's the stack.
- (1.1 KiB) Downloaded 147 times
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Padding lines with spaces?
Thanks to the good offices of Klaus . . .
Who "reminded" me about formattedWidth
- -
You can try this sort of thing:
And, obviously, append spaces until your formattedWidth is the one you require of each line, a bit like this:
Who "reminded" me about formattedWidth
- -
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Padding lines with spaces?
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
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.
-
- VIP Livecode Opensource Backer
- Posts: 9841
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Padding lines with spaces?
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn