Skip to main content

Get screen scale factor (screen.scale_factor)

Declaration​

local factor = screen.scale_factor()

Return Value​

  • factor
    Number indicating the ratio between device pixels and logical points (pt)

Description​

Use the return value to convert between the physical pixel resolution and the logical coordinate system.
Common values: 2.0 (most Retina iPhones and iPads), 3.0 (Plus/Max models and recent full-screen iPhones). Modern iPhones rarely use non-Retina 1.0 scale.
Combine with screen.size() to derive logical dimensions: width_pt = width_px / factor. Image search and color sampling still work with pixel sizes; only when working with point-based UI coordinates do you need to convert.

Example​

-- Convert pixel resolution to logical points
local w_px, h_px = screen.size()
local scale = screen.scale_factor()
local w_pt, h_pt = w_px / scale, h_px / scale

sys.log(string.format("Pixel resolution: %dx%d, scale: %.1fx, logical points: %.0fx%.0f", w_px, h_px, scale, w_pt, h_pt))

-- Adjust UI coordinates based on the scale factor
local x_point, y_point = 100, 200 -- Coordinates in points from Apple HIG or third-party UI specs
local x_px, y_px = x_point * scale, y_point * scale

touch.tap(x_px, y_px)

Note: The sample uses functions outside this chapter: sys.log, touch.tap