Skip to main content
Version: 9.0.2

sb.getStringSize

sb.getStringSize(font, font_size, string, width, data)

Calculate the area in pixels which the given string will occupy on the screen.

Parameters

ParameterTypeDescription
fontstringThe name of the font to render in
font_sizenumberThe size of the font to render in
stringstringThe string to render
widthnumberA clipping width (in pixels) for the string, used to calculate how many characters fit
dataobjectAn object containing additional options. See String Size Options Object for details.

String Size Options Object

KeyTypeDescription
lengthnumberNumber of characters to process. Default is to process the entire string.
spacingnumberLetter spacing parameter. Default is 0.
wrappingbooleanActivate wrapping logic. Default is false.

Return

An object with the following fields:

KeyTypeDescription
stringstringThe original string passed in
fontstringThe original font passed in
font_sizenumberThe original font size passed in
widthnumberThe original width passed in
heightnumberThe total height of the wrapped text in pixels
n_linesnumberThe number of lines the text fills when wrapped

Example

var options = {wrapping : true};
var t = sb.getStringSize('fonts/Roboto-ThinItalic.ttf', 72, 'How long is this string?', 600, options);