find fonts on device

Posted by coynage, Posted on August 29, 2011

2 votes

added code in comment to http://developer.anscamobile.com/reference/index/nativegetfontnames - just a simple program to run on device to list all native fonts on the device.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- findfonts function to list all device fonts
function findfonts()
        local _H = display.contentHeight
        local _W = display.contentWidth
        local g = display.newGroup()
        local columnPixel = 0
        local numRows = 80 -- <-------------------- play with this to fit device
        local numCols = 5 -- <-------------------- & play with that to fit device
        local fontHeight = round(_H/numRows,0)
                print("font height: ",fontHeight)
        local vPixelSkip = fontHeight - 1
        local fonts = native.getFontNames()
        local count, found_count = 0, 0
        for i,fontname in ipairs(fonts) do
                count = count+1
                j, k = string.find(fontname, "")                   
                if (j ~= nil) then
                        found_count = found_count + 1
                        -- print (found_count)
                        if found_count > numRows then
                                found_count = 0
                                columnPixel = columnPixel + (_W/numCols)
                        end
                        local obj = display.newText(fontname, columnPixel, (found_count - 1) * vPixelSkip, fontname, fontHeight)
                        g:insert(obj)
                        obj:setTextColor(127, 255, 255)
                end
        end
        print ("Font count: " .. count)
        local count_text = display.newText("Found " .. count .. " fonts.", columnPixel, (found_count) * vPixelSkip, fontname, 18)
        g:insert(count_text)
        count_text:setTextColor(255, 255, 127)
end
 
-- rounding function
function round(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
 
findfonts()