TextListener to stop from writing too many lines?

Hi Guys,

I’m looking for a method to count the number of lines in a newTextbox but I can’t seem to find a method to do so. I have a method to count the number of characters and prevent from writing too many but not the number of lines.

Anyone know of a method?

Thanks again,

Matt.

TextBoxes don’t have the concept of ‘lines’.

The concept of a ‘line’ is malleable (squishy).  i.e. It can change based on font character metrics (kerning, etc.),  font size and box width.  

The best you can do is count characters and make a judgement of your own how many lines there are.

i.e. Estimate 40 characters in a line and then, if you have 100 characters, you’ve got 3 lines.

Ah I will use this method instead thank you.

Any ideas why this isn’t working…

[lua]

local function textListener( event )

   local target = event.target

   local maxLen = target.maxLen

   local text = event.text

   

   if ( event.phase == “began” ) then

     print( text )

   else

if( text and string.len( text ) > maxLen ) then – aborts early if text is nil 

  target.text = string.sub( text, 61, maxLen ) 

end

   end

end

[/lua]

[lua]

–textBoxBuilding

  local textBoxBuilding = native.newTextField( 160, 146, 300, 40 )

  textBoxBuilding.size = 20

  textBoxBuilding:addEventListener( “userInput”, textListener )

  sceneGroup:insert( textBoxBuilding )

[/lua]

I keep getting this runtime error

29: attempt to compare nil with number

Thanks again,

Matt.

What is line 29?  You have two values that you’re doing a comparison with.  You need to determine which one is nil and why.

Rob

Hi Rob,

Line 29 is [lua]if( text and string.len( text ) > maxLen ) then – aborts early if text is nil [/lua]

Thanks,

Matt.

before you get to that line, print out text and string.len(text) and maxLen and make sure the values are what you’re expecting.

Rob

How do you advise I do this as i’m using Windows and Android at the moments?

Thanks Matt.

You will have to install on Android and use “adb logcat” to view the output from your tethered device.

See: http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

I have done this, but it doesn’t seem to print anything. Think this might be due to the second I click on the text field it has the runtime error.

Thanks,

Matt.

Did you set the field ‘maxLen’ on the textField instance?  We had this talk last time we talked about limiting # of characters, and again I don’t see you setting it.

-textBoxBuilding local textBoxBuilding = native.newTextField( 160, 146, 300, 40 ) textBoxBuilding.size = 20 textBoxBuilding:addEventListener( "userInput", textListener ) sceneGroup:insert( textBoxBuilding ) textBoxBuilding.maxLen = 100

Hi there, yes I believe this is the issue. I completely forgot to set it. Thanks again. Matt.

TextBoxes don’t have the concept of ‘lines’.

The concept of a ‘line’ is malleable (squishy).  i.e. It can change based on font character metrics (kerning, etc.),  font size and box width.  

The best you can do is count characters and make a judgement of your own how many lines there are.

i.e. Estimate 40 characters in a line and then, if you have 100 characters, you’ve got 3 lines.

Ah I will use this method instead thank you.

Any ideas why this isn’t working…

[lua]

local function textListener( event )

   local target = event.target

   local maxLen = target.maxLen

   local text = event.text

   

   if ( event.phase == “began” ) then

     print( text )

   else

if( text and string.len( text ) > maxLen ) then – aborts early if text is nil 

  target.text = string.sub( text, 61, maxLen ) 

end

   end

end

[/lua]

[lua]

–textBoxBuilding

  local textBoxBuilding = native.newTextField( 160, 146, 300, 40 )

  textBoxBuilding.size = 20

  textBoxBuilding:addEventListener( “userInput”, textListener )

  sceneGroup:insert( textBoxBuilding )

[/lua]

I keep getting this runtime error

29: attempt to compare nil with number

Thanks again,

Matt.

What is line 29?  You have two values that you’re doing a comparison with.  You need to determine which one is nil and why.

Rob

Hi Rob,

Line 29 is [lua]if( text and string.len( text ) > maxLen ) then – aborts early if text is nil [/lua]

Thanks,

Matt.

before you get to that line, print out text and string.len(text) and maxLen and make sure the values are what you’re expecting.

Rob

How do you advise I do this as i’m using Windows and Android at the moments?

Thanks Matt.

You will have to install on Android and use “adb logcat” to view the output from your tethered device.

See: http://docs.coronalabs.com/guide/basics/debugging/index.html

Rob

I have done this, but it doesn’t seem to print anything. Think this might be due to the second I click on the text field it has the runtime error.

Thanks,

Matt.

Did you set the field ‘maxLen’ on the textField instance?  We had this talk last time we talked about limiting # of characters, and again I don’t see you setting it.

-textBoxBuilding local textBoxBuilding = native.newTextField( 160, 146, 300, 40 ) textBoxBuilding.size = 20 textBoxBuilding:addEventListener( "userInput", textListener ) sceneGroup:insert( textBoxBuilding ) textBoxBuilding.maxLen = 100