Graphical text that can be drawn to a render target.
1 // Declare and load a font 2 auto font = new Font(); 3 font.loadFromFile("arial.ttf"); 4 5 // Create a text 6 auto text = new Text("hello", font); 7 text.setCharacterSize(30); 8 text.setStyle(Text.Style.Bold); 9 text.setColor(Color.Red); 10 11 // Draw it 12 window.draw(text);
$(FONT_LINK), $(TRANSFORMABLE_LINK)
$(U Text) is a drawable class that allows one to easily display some text with a custom style and color on a render target.
It inherits all the functions from $(TRANSFORMABLE_LINK): position, rotation, scale, origin. It also adds text-specific properties such as the font to use, the character size, the font style (bold, italic, underlined), the global color and the text to display of course. It also provides convenience functions to calculate the graphical size of the text, or to get the global position of a given character.
$(U Text) works in combination with the $(FONT_LINK) class, which loads and provides the glyphs (visual characters) of a given font.
The separation of $(FONT_LINK) and $(U Text) allows more flexibility and better performances: indeed a $(FONT_LINK) is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a $(U Text) is a lightweight object which can combine the glyphs data and metrics of a $(FONT_LINK) to display any text on a render target.
It is important to note that the $(U Text) instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a $(FONT_LINK) must not be destructed while it is used by a $(U Text).
See also the note on coordinates and undistorted rendering in $(TRANSFORMABLE_LINK).