Difference between revisions of "love.graphics.newText"

(Created page)
 
(Example)
 
(4 intermediate revisions by 3 users not shown)
Line 12: Line 12:
 
=== Returns ===
 
=== Returns ===
 
{{param|Text|text|The new drawable Text object.}}
 
{{param|Text|text|The new drawable Text object.}}
 +
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
text = love.graphics.newText( font, coloredtext )
 +
</source>
 +
=== Arguments ===
 +
{{param|Font|font|The font to use for the text.}}
 +
{{param|table|coloredtext|A table containing colors and strings to add to the object, in the form of <code>{color1, string1, color2, string2, ...}</code>.}}
 +
{{subparam|table|color1|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string1|A string of text which has a color specified by the previous color.}}
 +
{{subparam|table|color2|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string2|A string of text which has a color specified by the previous color.}}
 +
{{subparam|tables and strings|...|Additional colors and strings.}}
 +
=== Returns ===
 +
{{param|Text|text|The new drawable Text object.}}
 +
 +
=== Example ===
 +
<source lang="lua">
 +
local font = love.graphics.getFont()
 +
--regular text
 +
local plainText = love.graphics.newText(font, "Hello world")
 +
--colored text
 +
local coloredText = love.graphics.newText(font, {{1, 0, 0}, "Hello ", {0, 0, 1}, " world"})
 +
</source>
 +
 +
=== Example ===
 +
Create text and reflection:
 +
[[File:Text-Object.png|200px|thumb|right|Text object]]
 +
<source lang="lua">
 +
love.graphics.setFont (love.graphics.newFont (50))
 +
 +
font = love.graphics.getFont ()
 +
text = love.graphics.newText(font)
 +
height = font:getHeight( )
 +
text:add( {{1,1,1}, "Hello World!"}, 0, 0)
 +
text:add( {{0.8,0.8,0}, "Hello World!"}, 0, 0, 0, 1, -1, 0.32*height, 1.6*height, -0.4,0)
 +
 +
function love.draw()
 +
    love.graphics.draw (text, 10, 10)
 +
end
 +
</source>
 +
 +
=== Notes ===
 +
The color set by [[love.graphics.setColor]] will be combined (multiplied) with the colors of the text, when drawing the Text object.
  
 
== See Also ==
 
== See Also ==

Latest revision as of 13:31, 12 December 2023

Available since LÖVE 0.10.0
This function is not supported in earlier versions.

Creates a new drawable Text object.

O.png This function can be slow if it is called repeatedly, such as from love.update or love.draw. If you need to use a specific resource often, create it once and store it somewhere it can be reused!  



Function

Synopsis

text = love.graphics.newText( font, textstring )

Arguments

Font font
The font to use for the text.
string textstring (nil)
The initial string of text that the new Text object will contain. May be nil.

Returns

Text text
The new drawable Text object.

Function

Synopsis

text = love.graphics.newText( font, coloredtext )

Arguments

Font font
The font to use for the text.
table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.

Returns

Text text
The new drawable Text object.

Example

local font = love.graphics.getFont()
--regular text
local plainText = love.graphics.newText(font, "Hello world")
--colored text
local coloredText = love.graphics.newText(font, {{1, 0, 0}, "Hello ", {0, 0, 1}, " world"})

Example

Create text and reflection:

Text object
love.graphics.setFont (love.graphics.newFont (50))

font = love.graphics.getFont ()
text = love.graphics.newText(font)
height = font:getHeight( )
text:add( {{1,1,1}, "Hello World!"}, 0, 0)
text:add( {{0.8,0.8,0}, "Hello World!"}, 0, 0, 0, 1, -1, 0.32*height, 1.6*height, -0.4,0)

function love.draw()
    love.graphics.draw (text, 10, 10)
end

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text, when drawing the Text object.

See Also


Other Languages