iOS Question Text input size limit; debug and release modes [edited]

igodese

Member
Licensed User
Hi all,
I want to limit the size of text in an edit box. I've seen the solutions proposed in this post: https://www.b4x.com/android/forum/posts/560798/
but such code worked only in debug mode, not in release.
I made a small change:

B4X:
Sub CutText(Tf As TextField, MaxLength As Int)
    Tf.ResignFocus
    Tf.Text = Tf.Text.SubString2(0,MaxLength)
    Tf.RequestFocus
End Sub

Private Sub txtOnt_TextChanged (Old As String, New As String)
   If New.Length > 5 Then ' if string too long
       CallSubDelayed3(Me, "CutText", txtOnt, 5) ' reduce to the allowed size
'       CutText(txtOnt, 5) ' also works in immediate mode
   End If
End Sub

and this worked in debug AND in release mode.
Hope it will be useful for someone...

edited Jun 9, 2018 : it DOESN'T work if the textview is in password mode! Does anyone have an alternate solution for such case ?
 
Last edited:

tucano2000

Active Member
Licensed User
Longtime User
Great work @igodese. I was trying to solve this problem at several hours and found this code that worked perfectly. I do not understand why Debug and Release mode differ in performing this task.
 
Upvote 0

tucano2000

Active Member
Licensed User
Longtime User
Now let me contribute with my modified work to use mask text. I do not know if someone has done it this way but the color of the character changes from blue to red when the user types an illegal character and show a ToastMessage. You can change the code so that the different character of the mask does not appear in the TextField. But this way I think it was very interesting.

B4X:
Private Sub TextField1_TextChanged (Old As String, NewText As String)

 
    If NewText.Length<1 Then
        TextField1.TextColor = Colors.Blue
        Return
    End If

    If NewText.Length>7 Then 'Size Max to Textfield1
        CallSubDelayed3(Me, "CutText", TextField1, 7) 'Size Max to Textfield1
    Else
        TextField1.TextColor = Colors.Blue
        Dim Mask As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 'allowed characters
        Dim Textlist As List
        Textlist.Initialize
        Textlist.Clear
        For m = 0 To NewText.Length - 1
            For n=0 To Mask.Length - 1
                If NewText.SubString2(m,m+1)=Mask.SubString2(n,n+1) Then
                    Textlist.Add(NewText.SubString2(0,m+1))
                End If
            Next
        Next
        If Textlist.Size <> NewText.Length Then
            TextField1.TextColor = Colors.Red
             If NewText.Length>=Old.Length Then
                Dim PD1 As HUD
                PD1.ToastMessageShow("Not allowed character",False)
            End If
        End If
 
    End If


End Sub

Sub CutText(Tf As TextField, MaxLength As Int)
    Tf.ResignFocus
    Tf.Text = Tf.Text.SubString2(0,MaxLength)
    Tf.RequestFocus
End Sub
 
Last edited:
Upvote 0
Top