Edited January 15th, 2015.
This code is for those who, like me, don’t want to spend money on Glyph Designer, bmGlyph, Text Candy, or any other bitmap font tool for Corona SDK.
I ran into a piece of code written by Aaron Meriwether (a.k.a. ‘p120ph37’) that was modified by Michael Wilson who removed the legacy Sprite library and switched to a modular class. Albeit messy, this code had all of the features I was looking for. I simply cleaned it up a little bit, removed some unnecessary lines of code, fixed some issues with the alignment and, most importantly, added a dynamic font selection feature. This means devices with higher resolutions will use higher resolution images.
It’s very easy to use. Simply require the module, load a font, and print a string. Lines of text are handled as groups and they share the same properties.
Here’s the code example in the screenshot.
local bmf = require('bmf') local centerX = display.contentCenterX local centerY = display.contentCenterY local width = display.contentWidth local height = display.contentHeight local rect = display.newRect(centerX, centerY, width, height) rect:setFillColor(0.5,0.78,1) local font = bmf:load('SourceSansProSemibold18') local str = bmf:newString(font, '\<-- Right Align') str.align = 'right' str:translate(centerX, centerY - 27) local str2 = bmf:newString(font, '\<-- Center Align --\>') str2.align = 'center' str2:translate(centerX, centerY - 9) local str3 = bmf:newString(font, 'Left Align --\>') str3.align = 'left' str3:translate(centerX, centerY + 9)
All 3 strings have the same X coordinate but different alignments. The screenshot shows an iPhone 5 using a 2x resolution image. If we switch to an iPhone 6, it’ll use a 4x resolution image. Each image has its own ShoeBox / AngelCode font file (’.fnt’) with its own properties and dimensions.
You can optionally add a ‘group’ parameter at the beginning of the ‘newString’ function. You can also omit the ‘align’ parameter. The default is ‘left’.
local str = bmf:newString(group, font, 'Hello Corona!')
You can download the example here. (Note: The example uses the previous version of the code.)
http://www.mediafire.com/download/m5s96l0zy2tn4j8/Bitmap+Font.zip
You can view the code here.
https://github.com/jjsanchezramirez/Corona-SDK/blob/master/bitmapfont.lua