iOS Question [SOLVED] How to stop TextView scrolling? - weird behaviour observed.

JackKirk

Well-Known Member
Licensed User
Longtime User
I have been trying to sort out how to stop a TextView from scrolling within its view by using SizeTofit.

In the process I have set up a simple little test rig:
B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #MinVersion: 7
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
 
    Private TextView1 As TextView

End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    If TextView1.IsInitialized Then TextView1.RemoveViewFromParent
    TextView1.Initialize("")
    Page1.RootPanel.AddView(TextView1, 10, 10, Width - 20, Height - 20)
    TextView1.SetBorder(1, Colors.Black, 0)
    TextView1.Text = "garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage"
    TextView1.SizeToFit
End Sub

Private Sub Application_Background
 
End Sub

The weird thing is that when in Portrait the TextView scrolls (which I don't want it to do) BUT when in Landscape the TextView doesn't scroll (which is what I want to happen).

2 questions:
  1. Why the difference in behaviour between Portrait and Landscape?
  2. How can I get scrolling to stop in Portrait?
Thanks in anticipation...
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Why are you adding the view in the resize event? You should only change the size.
Erel,

I guess I was channelling my B4A experience:)

It's a bit beside the point though - I recoded the test rig as suggested:
B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #MinVersion: 7
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private TextView1 As TextView

End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    If Not(TextView1.IsInitialized) Then
        TextView1.Initialize("")
        Page1.RootPanel.AddView(TextView1, 10, 10, 10, 10)
        TextView1.SetBorder(1, Colors.Black, 0)
        TextView1.Text = "garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage garbage"
    End If
    TextView1.Width = Width - 20
    TextView1.SizeToFit
End Sub

Private Sub Application_Background
End Sub
With the same result:

The weird thing is that when in Portrait the TextView scrolls (which I don't want it to do) BUT when in Landscape the TextView doesn't scroll (which is what I want to happen).

2 questions:
  1. Why the difference in behaviour between Portrait and Landscape?
  2. How can I get scrolling to stop in Portrait?
Thanks in anticipation...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Please have a look at this 20 sec YouTube video:


Which demonstrates the "scrolling" behaviour I am observing on my iPhone 4S running iOS 9.3.1 which I understand is the latest and (not so) greatest.

Regards...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Anyway, SizeToFit is a native method. There is no way to configure it to scroll or to avoid scrolling.
I found the problem - SizeToFit seems to have some sort of rounding issue.

With a little fiddle TextView can actually be configured to avoid scrolling using SizeToFit.

If you change the lines in my test rig from:
B4X:
    TextView1.Width = Width - 20
TextView1.SizeToFit
To:
B4X:
    TextView1.Width = Width - 20
TextView1.SizeToFit
TextView1.Height = TextView1.Height + 1 '<<<< THE MAGIC LINE
My unwanted scrolling problem goes away.

This could explain why it doesn't do it in Landscape (presumably rounds the right way) and maybe why I see it on a 4S but Erel doesn't on a 5S (maybe different screen sizes producing different roundings?).

Regards...
 
Last edited:
Upvote 0
Top