I totally missed that. Thank you very much! One more point for the home team!!!
Hi Rob,
This is a great example…thanks for putting it out!!
Can you teach me a few things?
Re:
local deviceScale = (display.pixelWidth / display.contentWidth) * 0.5
field.textField.font = native.newFont( opt.font )
field.textField.size = opt.fontSize * deviceScale
Where did you get this math to set fontSize: opt.height * 0.67
And what’s the point of using “deviceScale” to modify the fontSize on the native field?
Also, is “finalize” something internally supported by Corona, or is that just your personal “cleanup” convention?
Also, I’m assuming you know that function args ARE local vars (ie equally fast), so I’m not sure what advantage you seek by doing this:
function widget.newTextField(options)
local customOptions = options or {}
local opt = {}
Can you illuminate?? For example, whats wrong with one table instead of 3 to set options on constructed objects?
Oh, I guess you are afraid of overwriting the source table with defaults you set (ie pass-by-reference artifacts & all that)…is that the point? Wouldn’t two tables suffice?
Thanks again for a great start here.
Dewey
I chose 67% of the boxes overall height for the text height by wanting to leave some white space above and below the text. This seems to be what’s common on devices.
The deviceScale is part of getting native objects to produce text that’s on the same point size as display.newText. In Corona since display.newText() objects live in the OpenGL world, they are subject to having their size determined by the content area. So let’s say you’re app is a 320x480 app and you say you want a 32 point font, it’s going to be 10% of the height (in landscape) 32 points out of 320 points. Now if your device is really a 640x960 device, Corona SDK automatically adjusts display.newText() to actually make that 32 point font a 64 pixel font on retina phones or a 32 pixel font on an iPhone 3Gs for instance. On a Retina iPad to maintain the same scale it would need to be a 128 pixel high font (this is why we use content areas and “points” instead of pixels when talking about the content area). This is great and Corona does what we want.
However for native.newTextField()'s and native.newTextBox()'s, the font’s don’t scale. So that 32 point font is going to be 32 pixels on a 3GS, 32 pixels on a retina phone and 32 pixels on a retina iPad. This doesn’t work well. So the device Scale is a math trick to make the native font’s scale based on the device it’s actually running on. So if you’re on a device somewhere in between it will maintain the rough percentage of height as display.newText.
Finally… finalize is a Corona SDK feature designed to help people clean up objects when they are being removed. Let’s say you’re spawning physics objects, you can give each object a finalize event handler that would automatically remove its physics body when its display object is being destroyed for instance. It’s a pretty cool feature.
Rob