Can I determine if a scrollview needs scrolling?

Hi,
I have a scrollview … it works fine. About half the time, the text I display in it fits completely
in the box of the scrollview, and there is no need to scroll (and nothing to scroll to).
The other half of the time, I have more text … to the point where it logically extends beyond
the bottom of the box, and the user has to scroll to see it all.

I’d like to have code like:

if my_scrollview.can_be_scrolled then
     info.text = "You can scroll the text to see more"
end

I.e., I want to dynamically tell the user that there is more text than is visible. (I don’t want to tell them all the time … if they believed the message, half the time they’d be frustrated trying to scroll!)

BUT … I dumped (#1) the complete scrollview object for about a dozen cases (6 where text fit, 6 where it didn’t), and compared them to each other. There were no obvious internal fields to tell me anything useful (e.g., # of text lines, or, ‘text exists below bottom of window’).

So…is this possible to determine?

thanks,

Stan

  1. the “dump” is a recursive table printer

You could write a function that simply checks if the scrollWidth/scrollHeight is larger than the width/height of the scrollView itself. If true, then the scrollable content extends past the visible area of the scrollView.

what i usually use to ensure the text is on-screen in using enterFrame listener, and check whether the text is within the screen. something like that

	local scrollView = YOUR_SCROLL_VIEW
	local textObject = display.newText(...)
	local function onFrame(event)
		
		if( textObject.contentBounds.yMax > scrollView._view.y ) then .... end
		
		-- Lock the scroll view
		-- scrollView:setIsLocked( true )
		
	end
	Runtime:addEventListener( "enterFrame", onFrame )

	-- Remember to remove this runtime listener
	-- Runtime:removeEventListener( "enterFrame", onFrame )
1 Like

@XeduR thanks, but no … I tried to point out that I’d compared the object fields in scrollable/nonscrollable cases, and that includes the scrollWidth and scrollHeight values … they’re identical across all 12 cases I examined :frowning:

Edit:
I did not, however, compare the component newText object fields (reference to my reply to xosu),
so my initial reply may be hasty, XeduR. thanks again!

@yosu thanks…I’ll look at that approach.

My text/scroll is a collection of multiple text objects (“paragraphs”) … from an example posted by, IIRC, Rob Miracle. At this time, I can’t recall why I didn’t use just a single text object instead of multiple ones.
[wait]

So, I removed the weird multiple-paragraph code, and now have a single newText object … the text still displays just fine. Now I’ll investigate your approach, thanks! (That will take a bit more time :slight_smile:

Oddly, checking a scrollview’s scrollHeight gave me weird results. In all tests but 1, I got the expected value (that I created the scrollview width). In one test (source code lost due to changes, sigh), I got varying non-integer sizes that roughly corresponded with the size of the text being displayed. Weird.

And, the contentBounds.yMax was consistently higher/larger than it should have been (e.g., larger than the scrollview height even when the text fit completely within the scrollview)!

But … what I was finally able to use was this:

if t_help.height >= h then
   show_status ("(Scroll up/down if needed)")

end

t_help is my display.newText object, and ‘h’ is the height value I used to create the scrollview (and is the same as scrollview._view._scrollHeight).

BTW, the above code was correct in all but one case … where the last line of the text ended at
the last visible section of the scrollview … AND that line ended in a ‘newline’ character (\n).
It appears that the ‘height’ value was too large by 13 in that case. If I strip the trailing newline,
the height becomes correct, and the code is perfect.

I sure wish there was a way to get all the fields of a table/object displayed (e.g., a newText object appears to Lua to have only a few fields, and contentBounds is not one of them)!

thanks all!

Stan

the values in contentBounds are actually the x/y on screen, so it will change if object moves. It also took me a while to fiddle and figure out all those values :slight_smile: Good knowledge