Counting lines.

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Counting lines.

Post by Kaubs » Fri Oct 28, 2011 11:05 pm

Looking for some advice.

I have a field in which resides a lot of text data in paragraph formatting with quite a few breaks. I am attempting to calculate the absolute amount of characters including the spaces to the last character in the field. Could you guys please share the most accurate or best methods you have come up with or thought of to get a good count?

I count the chars in a field currently and then build a scroller based off of the count. It works but something is wrong in that at the end of the dynamic count sometimes I will have nearly an extra retina screen pixel area of blank space once my scroller is built.

Thanks guys!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Counting lines.

Post by Dixie » Fri Oct 28, 2011 11:13 pm

Kaubs...

Would you like to post your code ?... I'm not too sure that I understand exactly what you mean.

be well,

Dixie

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: Counting lines.

Post by Kaubs » Fri Oct 28, 2011 11:18 pm

Sure, just a few mins while I chop it up a little. Keep in mind it will be missing a lot of elements and will not be functional please. Ill do my best to explain it better if you have more questions once you see the code.

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: Counting lines.

Post by Kaubs » Fri Oct 28, 2011 11:22 pm

Please forgive me as my coworker and I are still fairly new to LC...I'm sure this will look a bit messy.

Code: Select all

local sScrollerId
Global NumberOfLines
Global ReleaseFlowType

on preOpenCard
         move grp "DataGrid 1" on card "rels4.5" to 282,428 in 1 ticks
   doReadtemplate ----for reading from a database to populate a field in group "datagrid 1"

      
   
    
    if the environment is not "mobile" then
        exit preOpenCard
     end if
     --math takes place here to calculate how far we are going to allow scrolling
     --Dynamic Scroll
     put the number of chars of field id 12052 on card "Rels4.5" into field "ScrollCount" on card "Rels4.5" 
     Divide field "ScrollCount" by 45
     multiply field "ScrollCount" by 32
     add 64 to field "scrollCount"
     put line one of field "ScrollCount" into NumberOfLines
     --end math
    
    set the unboundedHScroll of group "DataGrid 1" to false
    set the unboundedVScroll of group "DataGrid 1" to true
    
    iphoneControlCreate "scroller"
    put the result into sScrollerId
    
    -- general properties
    iphoneControlSet sScrollerId, "visible", "true"
    iphoneControlSet sScrollerId, "canBounce", "true"
    iphoneControlSet sScrollerId, "pagingEnabled", "false"
    iphoneControlSet sScrollerId, "canScrollToTop", "true"
    iphoneControlSet sScrollerId, "indicatorStyle", "black"
    
    
    -- contentRect refers to the rect of the field that appears within the scroll control  
    -- the last 2 values in the rect are the width and height of the field that is being scrolled
    -- the last value can be set programatically or be hard coded (as I have here) to account for more or less 
    --  text in the field.  
    
    iphoneControlSet sScrollerId, "contentRect",  (0, 0, 600, NumberOfLines)  

    -- set rect of the scroller area
    -- vary these values to make the scroller control appear just where you want it to
    -- in this case it appears 88 px from the top of the screen, and is positioned up against the other 3 edges of the screen.  
    -- To move the bottom of the scrolling area upwards change the last value
    -- of the following line of code e.g. change "the bottom of this card " to "the bottom of this card -88" or whatever

    iphoneControlSet sScrollerId, "rect", (the left of this card, the top of this card + 150, the right of this card, the bottom of this card - 245) 

end preOpenCard

on closeCard
      --Moar Dynamic Scroll
   put empty into NumberOfLines
   put empty into field "ScrollCount"
   --End Dynamic Scroll
   if the environment is not "mobile" then
      exit closeCard
   end if
   
   iphoneControlDelete sScrollerId
end closeCard

on scrollerDidScroll OffsetX, OffsetY
    set the hScroll of group "DataGrid 1" to OffsetX
    set the vScroll of group "DataGrid 1" to OffsetY
end scrollerDidScroll

-- following stuff added for completeness --
on doAction whatAction
   iphoneControlDo sScrollerId, whatAction
end doAction

on doSetProp Prop, theValue
   iphoneControlSet sScrollerId, Prop, theValue
end doSetProp


Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Counting lines.

Post by Dixie » Fri Oct 28, 2011 11:52 pm

Kaubs...

Code: Select all

 iphoneControlSet sScrollerId, "contentRect",  (0, 0, 600, NumberOfLines) 
Are you mixing up some values here... the first three items of the 'contentRect', but in this case the content of the NumberOfLines variable will be treated as a pixel value... It seems to me that from the above line you wish to be able to just scroll through a certain number of lines...

So, say you wish to scroll only down to, say line 35 of your field... then

Code: Select all

put the textheight of fld "whateverName" * 35 into heightToScroll
iphoneControlSet sScrollerId, "contentRect",  (0, 0, 600, heightToScroll) 
might work for you...

be well

Dixie

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4178
Joined: Sun Jan 07, 2007 9:12 pm

Re: Counting lines.

Post by bn » Sat Oct 29, 2011 12:06 am

Hi Kaubs,

I don't quite get what you are after either. If I understand you correctly you want the amount of pixel the text has.
You can either do it as Dixie said and multiply the textHeight by the number of lines. Or you could ask for the formattedHeigth, formattedWidth, formattedRect.
It returns a pixel value. This way you could change the font later on and don't have to worry about textHeight.
A nice thing about formattedxxx is you can get it for parts of text

e.g. put the formattedHeight of line 1 to 10 of field "xyz" into temp
or put the formattedHeight of char 1 to 150 of field 1 into field 2

Kind regards

Bernd

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: Counting lines.

Post by Kaubs » Mon Oct 31, 2011 6:26 pm

Thanks for the responses! We are still tweaking it a little. The idea is that text is read into a field from a database, lines of text are then calculated and multiplied by the text height in order to always create a dynamic scroll area regardless of how much text is loaded each time from the database. It works on almost all of my fields except in a couple cases it glitches and displays a lot of empty space. I'd figured it was something I messed up on coding.

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: Counting lines.

Post by Kaubs » Mon Oct 31, 2011 9:43 pm

Ok, an update. I originally wanted to use "the number of lines of fld" but every time I did it was giving a really low number. The number of lines method would work great except I seem to have a fatal flaw happening which I chose to investigate...I cannot seem to find a way around this to make the number of lines work. The problem with number of chars is that sometimes we get a decimal amount because there are not an absolute amount of chars to divide by per line on each line.

Whats happening:
I have injected data into a sqlite DB.
sqlite DB is read and spits text into a field in LC.
Fields visibly word wrap but the contents show 1 line of text per paragraph on the property inspector (thus giving a low number of lines)

The data entered into the db was put in from a document so each paragraph shouldn't have been a single line...still not sure if the issue is sqlite or LC though and I don't want to point fingers until I hear a little input. What do you guys think? Am I being clear?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4178
Joined: Sun Jan 07, 2007 9:12 pm

Re: Counting lines.

Post by bn » Mon Oct 31, 2011 10:05 pm

Hi Kaubs,

try this in a button:

Code: Select all

on mouseUp
   put the number of lines of field 1 && the number of lines of the formattedText of field 1 into field 2
end mouseUp
of course you would want 2 fields: field 1 has your text and field 2 to put the result into.

For Livecode a line is everything that is delimited by the linedelimiter, the same as a paragraph in other words. It is linefeed as default. If the dontWrap of a field is false the text will wrap and you see many 'lines'. Those are the lines the formattedText returns.


If you want to go for lines that is. You also have the option to go for the formattedHeigth, formattesWidth. Just play around with it. It sounds a bit complicated but that is the way it is.

Kind regards

Bernd

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: Counting lines.

Post by Kaubs » Mon Oct 31, 2011 10:19 pm

Thank you both! BN thats exactly what I needed. I can now properly calculate off of lines of text. Though I do have one more question. It does calculate for empty rows right? As in...paragraph br br paragraph. It would see the lines in both paragraphs and also count the breaks?

Thanks!

Kaubs
Posts: 124
Joined: Wed Jun 29, 2011 3:55 am

Re: Counting lines.

Post by Kaubs » Mon Oct 31, 2011 10:26 pm

Either way, seems to work perfectly every time dynamically so thank you again!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4178
Joined: Sun Jan 07, 2007 9:12 pm

Re: Counting lines.

Post by bn » Mon Oct 31, 2011 10:29 pm

Hi Kaubs,
It does calculate for empty rows right? As in...paragraph br br paragraph. It would see the lines in both paragraphs and also count the breaks?
yes.

Kind regards

Bernd

Mantra: FormattedWidth, FormattedHeight. FormattedText, FormattedRect. Formatted is your friend... :)

ancient wisdom:
The Tao you see is not the true Tao unless you ask for the formattedTao...

tzenobite
Posts: 57
Joined: Sun Dec 04, 2011 3:59 pm

Re: Counting lines.

Post by tzenobite » Mon Apr 09, 2012 6:46 pm

hi, i'm experiencing a strange behaviour of my scrollers
i got a stack with two scroller in two different cards
both controls works well with the code i found here
the strange thing is that the second control, with his own variables names, field names, group names and so on, uses the rect of the other control :shock:
the first control is:

Code: Select all

set the unboundedHScroll of group "scroller" to false
set the unboundedVScroll of group "scroller" to true
put the formattedheight of fld "reader"  into altezzareader
set the rect of cd fld "reader" to (0,50,320,altezzareader+50)
    
    iphoneControlCreate "scroller"
    put the result into sScrollerId
    iphoneControlSet sScrollerId, "visible", "true"
    iphoneControlSet sScrollerId, "canBounce", "true"
    iphoneControlSet sScrollerId, "pagingEnabled", "false"
    iphoneControlSet sScrollerId, "canScrollToTop", "true"
    iphoneControlSet sScrollerId, "indicatorStyle", "white"

     -- contentRect refers to the rect of the field that appears within the scroll control  
     iphoneControlSet sScrollerId, "contentRect",  (0,0,320,altezzareader)  

    -- set rect of the scroller area
   iphoneControlSet sScrollerId, "rect", (0,50,320,480)
the second control is:

   

Code: Select all

 set the unboundedHScroll of group "indicescroller" to false
    set the unboundedVScroll of group "indicescroller" to true
put the formattedheight of fld "indice"  into altezzareader
--put "" into fld "righe"
set the rect of cd fld "indice" to (0,50,320,altezzareader+50)
    
    iphoneControlCreate "scroller"
    put the result into indicereader

    -- general properties
    iphoneControlSet indicereader, "visible", "true"
    iphoneControlSet indicereader, "canBounce", "true"
    iphoneControlSet indicereader, "pagingEnabled", "false"
    iphoneControlSet indicereader, "canScrollToTop", "true"
    iphoneControlSet indicereader, "indicatorStyle", "white"
    
    
    -- contentRect refers to the rect of the field that appears within the scroll control  
    -- the last 2 values in the rect are the width and height of the field that is being scrolled
    -- the last value can be set programatically or be hard coded (as I have here) to account for more or less 
    --  text in the field.  
    
    iphoneControlSet indicereader, "contentRect",  (0,0,320,altezzareader)  

    -- set rect of the scroller area
    -- vary these values to make the scroller control appear just where you want it to
    -- in this case it appears 88 px from the top of the screen, and is positioned up against the other 3 edges of the screen.  
    -- To move the bottom of the scrolling area upwards change the last value
    -- of the following line of code e.g. change "the bottom of this card " to "the bottom of this card -88" or whatever

    iphoneControlSet indicereader, "rect", (0,50,320,400)
the result is that the control covers all the screen from 50pixels from the top to the bottom of the screen, like a overlay, when i scroll the field to the bottom of the list the contento of the field "slide" over the buttons on the bottom of the card and reveals it, but if i tap outside the teorethical rect of the control the scroller don't scroll
i need a scoller with a rect of 0,50,320,400 for a really higher field desperately!
so the rect of the control draws the area within the fingers can scroll the field but DON'T draws the visibile rect of the field under the scroller
it seems like another rect is missing, like the iphoneControlSet "rect" set the interactive area but not the viewable area of the scroller content either

i'm exausted, i don't have any idea about it, but i NEED to solve this... i need a scoller with a rect of 0,50,320,400 for a really higher field desperately!

at least, if someone would be so kind to make me a little stack to let me see where i'm wrong...

i'm not sure i'm clear, it's not so easy to me write in english, i hope you understand :-)

thanks in advance once again :-)

update: some images to make it clear
how many hypercardist are needed to change a light bulb? one to actually do it while at least a dozen more are finding out a slightly different way to do it

proudly hypertalking since 1989

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4178
Joined: Sun Jan 07, 2007 9:12 pm

Re: Counting lines.

Post by bn » Mon Apr 09, 2012 9:03 pm

Hi tzenobite,

I don't see images.

Apart from that do you delete the scroller of card 1 on "closeCard" and create the scroller in a "preopenCard" handler on the second card?

Alternatively you could probably "hide" the scroller on "closeCard"

If you don't delete the scroller from card 1 then it is still active on card 2 when you go there.

From what I understand from your post that could be the problem. If I understand you correctly both scrollers work alright on their own. Only if you go from card 1 to card 2 the probem appears. Then it is probably the above mentioned problem.

I always create scrollers on "preopenCard" and delete them on "closeCard". That way there is no way to get confused what scrollers are active from what card. And creating and deleting scrollers is very fast.

Kind regards

Bernd

tzenobite
Posts: 57
Joined: Sun Dec 04, 2011 3:59 pm

Re: Counting lines.

Post by tzenobite » Mon Apr 09, 2012 9:23 pm

sorry, i'm trying to upload the images but the adsl here is very slow
i don't make more than a scroller, actually i start the test on the simulator via lc, go from the home card directly to the card with the scroller and the scroller doesnt' work
i've just seen that the other controller don't work well, too: if i resize the "rect" also in this other scroller only the interactive area is resized, the viewable part of the field remains the same

try again with the images
Attachments
3.png
2.png
1.png
how many hypercardist are needed to change a light bulb? one to actually do it while at least a dozen more are finding out a slightly different way to do it

proudly hypertalking since 1989

Post Reply