Line numbers in text box
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 23
- Joined: Sun Mar 03, 2013 1:09 am
Line numbers in text box
Hello
I am able to find the total number of lines in my text box but is there a way to have each line number displayed at the beginning of each line? Thanks in advance!
Rod
I am able to find the total number of lines in my text box but is there a way to have each line number displayed at the beginning of each line? Thanks in advance!
Rod
Re: Line numbers in text box
Hi.
No problem having line numbers precede each line, but this is not a native property.
Does this, maybe in a button script somewhere help to get you started?
Can you do this yourself? Do you see how it works? This also means managing the field contents when you add or subtract lines. Write back if that is a problem.
Craig Newman
No problem having line numbers precede each line, but this is not a native property.
Does this, maybe in a button script somewhere help to get you started?
Code: Select all
on mouseup
put field "yourField" into temp
repeat with y = 1 to the number of lines of temp
put y && line y of temp into line y of temp
end repeat
put temp into fld "yourField"
end mouseUp
Craig Newman
Re: Line numbers in text box
To avoid manipulating your fld use 2 flds
Put this into your text fld:
The fld Line Number is set to the left of your text fld and full of... numbers
Now I have to figure out how to populate fld Line Numbers on the fly.
Simon
Oh, the text formatting has to be the same in both flds (font size and line height 'n stuff)
Put this into your text fld:
Code: Select all
on scrollbarDrag newValue
set the vScroll of fld "Line Number" to newValue
end scrollbarDrag
Now I have to figure out how to populate fld Line Numbers on the fly.
Simon
Oh, the text formatting has to be the same in both flds (font size and line height 'n stuff)
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Line numbers in text box
OK got it:
That populates the Line Number field
and in the text fld:
There how's that
Simon
EDIT: RATS! only works if the dont wrap of the text fld is selected
Code: Select all
on openCard
put "" into fld "Line Number"
repeat for the number of lines in fld "Text Field"
add 1 to x
put x & cr after fld "Line Number"
end repeat
delete the last char of fld "Line Number"
end openCard
and in the text fld:
Code: Select all
on scrollbarDrag newValue
set the vScroll of fld "Line Number" to newValue
end scrollbarDrag
on returnInField
put the last line of fld "Line Number" into tLine
add 1 to tLine
put cr & tLine after fld "Line Number"
pass returnInField
end returnInField
on enterInField
returnInField
end enterInField

Simon
EDIT: RATS! only works if the dont wrap of the text fld is selected
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Line numbers in text box
Ok once more then I may shut up.
Now you can have either don't wrap selected or not.
Simon
Now.... fix the assumption

Code: Select all
on openCard
put "" into fld "Line Number"
put the formattedHeight of fld "Text Field" into tPix
put round(tPix /16) into tPix --assumes a fixed line height of 16
repeat with x = 1 to tPix
put x & cr after fld "Line Number"
end repeat
delete the last char of fld "Line Number"
end openCard
Simon
Now.... fix the assumption

I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: Line numbers in text box
Hi Rod,
as Simon pointed out if you put the line number into the text field you will change the text and consequently how it breaks.
To avoid that I would use two fields as Simon proposed.
To calculate the line numbers of a wrapped text it is easiest to use the formattedText of a field. formattedText gives you the text as it is displayed but each displayed line has a return at the end.
Usually in LiveCode a line is considered all the text until a return is met. In other contexts this is also called a paragraph.
the code for a button to calculate line numbers:
place the two fields of the same height next to each other, hide the vScrollbar in the property inspector for field "line number" and put Simons script into the field of your text
Kind regards
Bernd
as Simon pointed out if you put the line number into the text field you will change the text and consequently how it breaks.
To avoid that I would use two fields as Simon proposed.
To calculate the line numbers of a wrapped text it is easiest to use the formattedText of a field. formattedText gives you the text as it is displayed but each displayed line has a return at the end.
Usually in LiveCode a line is considered all the text until a return is met. In other contexts this is also called a paragraph.
the code for a button to calculate line numbers:
Code: Select all
on mouseUp
put the formattedText of field "fText" into tText -- or whatever the name of your text field is
-- set the numberformat to "000" -- optional to display e.g. 001 instead of 1
put 0 into tCounter
put "" into tCollect
repeat for each line aLine in tText
add 1 to tCounter
put tCounter & cr after tCollect
end repeat
delete last char of tCollect -- a return
put tCollect into field "line number"
set the vScroll of field "fText" to 0
end mouseUp
Code: Select all
on scrollbarDrag newValue
set the vScroll of fld "Line Number" to newValue
end scrollbarDrag
Bernd
-
- Posts: 23
- Joined: Sun Mar 03, 2013 1:09 am
Re: Line numbers in text box
Thanks for your help everyone! I have it up and functioning perfectly. Slowly but surely getting the swing of things. Your assistance is truly appreciated.