Tutorial: New Native Text Input Features. Can't get it right

I’ve recently got access to the daily builds and finally I’m able to play with the new native.newTextField() (font) sizing features.

Unfortunately I’m not able to make it quite work. I’m trying to make three objects:

display.newText() object, a native.newTextField() object and a native.newTextBox() object having the same font size.

The display.newText() and the native.newTextBox() seems to show the same font size, but the

The tutorial is a bit fuzzy when it comes to font priority sizing, it says:

The second function (object:resizeHeightToFitFont()) is useful when the font size is the higher priority, and the field can be a variable size to accommodate the font size.

local textField = native.newTextField( 160, 240, 280, 40 ) textField.text = "Hello World"

Ok…

-- Change the text field's font size to the system default textField.size = 60

This part makes no sense to me. How is setting the height to 60 making the field use the system default font?

-- Resize the text field's height to best fit the font textField:resizeHeightToFitFont()

Ok.

In the docs for object:resizeHeightToFitFont() it says:

-- Change the text field's font size to the system default textField.size = nil

Trying to understand as best as I can I take it that there is an error in the tutorial and that the docs are right: That to make the field resize to the font is done by setting the size to nil and then call resizeHeightToFitFont().

But I’ve tried this and it doesn’t seem to work, so I’m a bit confused. Heres my complete test app code:

\_W = display.contentWidth \_H = display.contentHeight local widget = require("widget") label = display.newText("Label:", \_W\*0.1, \_H\*0.3, native.systemFont, 40) label.anchorX = 0.0 textField = native.newTextField(\_W\*0.1, \_H\*0.35, \_W\*0.4, 150) textField.font = native.newFont(native.systemFont, 40) textField.size = nil textField:resizeHeightToFitFont() textField.anchorX = 0.0 textBox = native.newTextBox(\_W/2, \_H\*0.4, \_W\*0.8, \_H\*0.2) textBox.font = native.newFont(native.systemFont, 40) textBox.isEditable = true textBox.anchorY = 0.0

When I run this on an (Android) device I get the attached screen (after entering “Label” in the edit field/box):

font_sizes.png

Is the tutorial or the docs right? What am I doing wrong?

Could it be that both the docs and the tutorial are wrong?

I tried to set the .height = nil and it looks promising:

textField = native.newTextField(\_W\*0.1, \_H\*0.35, \_W\*0.4, 150) textField.font = native.newFont(native.systemFont, 40) textField.height = nil textField:resizeHeightToFitFont()

Or a third possibility…

textField.size = 60 really  does mean “set the  font size to 60”.

But what about the font type then?

No. I don’t get it.

Hi @runewinse,

Yes, as noted in the docs, “textField.size” sets the font size of the field, not the width or height:

http://docs.coronalabs.com/daily/api/type/TextField/size.html

Then, in your example one post above, I presume what’s happening is this:

  1. You create the text field, setting its position/width/height as normal.

  2. You set its font (.font) to the native system font in size 40.

  3. You attempt to set the text field’s height (.height), but that’s not valid (see here: http://docs.coronalabs.com/daily/api/type/TextField/index.html)..)

  4. Then you resize the field’s height to fit the font, which should work fine, and the box should span nicely around the 40-size font you put in there.

Brent

Ok, so “textField.size = 60” really  does  mean “set the  font  size to 60”.

But what about my second question about where the font type  fits into this way of doing it?

Should one do something like this?

textField.font = native.newFont(native.systemFont, 40) textField.size = 40 textField:resizeHeightToFitFont()

Doing this, you have already specified the font size so why on earth should it be neccesary to re-specify the font size by setting textField.size?

Hi @runewinse,

I’m not sure what you mean by “font type”. Style? What the text reads (letters)? The font face/family?

You probably don’t need to re-specify the font size using .size. That is just an optional method.

Brent

Maybe this makes my question any clearer:

textField.font = native.newFont( native.systemFont , 40)

Exactly, the .font property, along with native.newFont(), lets you use a custom font for the input field, in the specified size. I’m not following what your specific question is.

Could it be that both the docs and the tutorial are wrong?

I tried to set the .height = nil and it looks promising:

textField = native.newTextField(\_W\*0.1, \_H\*0.35, \_W\*0.4, 150) textField.font = native.newFont(native.systemFont, 40) textField.height = nil textField:resizeHeightToFitFont()

Or a third possibility…

textField.size = 60 really  does mean “set the  font size to 60”.

But what about the font type then?

No. I don’t get it.

Hi @runewinse,

Yes, as noted in the docs, “textField.size” sets the font size of the field, not the width or height:

http://docs.coronalabs.com/daily/api/type/TextField/size.html

Then, in your example one post above, I presume what’s happening is this:

  1. You create the text field, setting its position/width/height as normal.

  2. You set its font (.font) to the native system font in size 40.

  3. You attempt to set the text field’s height (.height), but that’s not valid (see here: http://docs.coronalabs.com/daily/api/type/TextField/index.html)..)

  4. Then you resize the field’s height to fit the font, which should work fine, and the box should span nicely around the 40-size font you put in there.

Brent

Ok, so “textField.size = 60” really  does  mean “set the  font  size to 60”.

But what about my second question about where the font type  fits into this way of doing it?

Should one do something like this?

textField.font = native.newFont(native.systemFont, 40) textField.size = 40 textField:resizeHeightToFitFont()

Doing this, you have already specified the font size so why on earth should it be neccesary to re-specify the font size by setting textField.size?

Hi @runewinse,

I’m not sure what you mean by “font type”. Style? What the text reads (letters)? The font face/family?

You probably don’t need to re-specify the font size using .size. That is just an optional method.

Brent

Maybe this makes my question any clearer:

textField.font = native.newFont( native.systemFont , 40)

Exactly, the .font property, along with native.newFont(), lets you use a custom font for the input field, in the specified size. I’m not following what your specific question is.