17 August 2017
Introducing new font metrics APIs, plus a new plugin
Corona’s engineers have added two new APIs for getting information about fonts, with the intent of helping you better align text in your apps. We’ve also added a new plugin that you might want to take advantage of.
Font metrics APIs
Starting with daily build 2017.3121, there is a new API call, graphics.getFontMetrics(), which returns a table that contains values like the height of the font, font leading, and the ascent/descent values for the font. Ascent is the amount of recommended space above the baseline for single-spaced text, while descent is the suggested space below the baseline for single-spaced text. Leading is the recommended additional space between lines of text.
In addition, all TextObject objects (those created via display.newText()) have a new read-only property called baselineOffset which lets you adjust the position of the object so that you can easily align it with other objects, including graphics or other text objects that may be constructed with different fonts or font sizes.
Consider this output:
By using the baselineOffset property, you can easily align the baseline of each text object with the bottom of the gem for a more appealing display!
Battery State plugin
We also introduced a new plugin that lets you get the current state of the device’s battery. It offers an event-based update callback where you receive an event when the battery state changes (charging, unplugged, etc.). You can also call the batteryState.getState() function to get a one-time status on the device’s battery.