Skip to content

class SF::Text
inherits SF::Transformable #

Graphical text that can be drawn to a render target

SF::Text is a drawable class that allows to easily display some text with custom style and color on a render target.

It inherits all the functions from SF::Transformable: 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 and strike through), the text color, the outline thickness, the outline color, the character spacing, the line spacing 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.

SF::Text works in combination with the SF::Font class, which loads and provides the glyphs (visual characters) of a given font.

The separation of SF::Font and SF::Text allows more flexibility and better performances: indeed a SF::Font is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a SF::Text is a lightweight object which can combine the glyphs data and metrics of a SF::Font to display any text on a render target.

It is important to note that the SF::Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a SF::Font must not be destructed while it is used by a SF::Text (i.e. never write a function that uses a local SF::Font instance for creating a text).

See also the note on coordinates and undistorted rendering in SF::Transformable.

Usage example:

# Declare and load a font
font = SF::Font.from_file("arial.ttf")

# Create a text
text = SF::Text.new("hello", font)
text.character_size = 30
text.style = SF::Text::Bold
text.color = SF::Color::Red

# Draw it
window.draw text

See also: SF::Font, SF::Transformable

Included modules

SF::Drawable

Class methods#

.new(string : String, font : Font, character_size : Int = 30) #

Construct the text from a string, font and size

Note that if the used font is a bitmap font, it is not scalable, thus not all requested sizes will be available to use. This needs to be taken into consideration when setting the character size. If you need to display text of a certain size, make sure the corresponding bitmap font that supports that size is used.

  • string - Text assigned to the string
  • font - Font used to draw the string
  • character_size - Base size of characters, in pixels
View source

.new #

Default constructor

Creates an empty text.

View source

Methods#

#character_size : Int32 #

Get the character size

Returns: Size of the characters, in pixels

See also: character_size=

View source

#character_size=(size : Int) #

Set the character size

The default size is 30.

Note that if the used font is a bitmap font, it is not scalable, thus not all requested sizes will be available to use. This needs to be taken into consideration when setting the character size. If you need to display text of a certain size, make sure the corresponding bitmap font that supports that size is used.

  • size - New character size, in pixels

See also: character_size

View source

#color : Color #

Get the fill color of the text

Returns: Fill color of the text

See also: fill_color=

DEPRECATED: There is now fill and outline colors instead of a single global color. Use fill_color() or outline_color() instead.

View source

#color=(color : Color) #

Set the fill color of the text

By default, the text's fill color is opaque white. Setting the fill color to a transparent color with an outline will cause the outline to be displayed in the fill area of the text.

  • color - New fill color of the text

See also: fill_color

DEPRECATED: There is now fill and outline colors instead of a single global color. Use fill_color=() or outline_color=() instead.

View source

#dup : Text #

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.

View source

#fill_color : Color #

Get the fill color of the text

Returns: Fill color of the text

See also: fill_color=

View source

#fill_color=(color : Color) #

Set the fill color of the text

By default, the text's fill color is opaque white. Setting the fill color to a transparent color with an outline will cause the outline to be displayed in the fill area of the text.

  • color - New fill color of the text

See also: fill_color

View source

#finalize #

Virtual destructor

View source

#find_character_pos(index : Int) : Vector2f #

Return the position of the index-th character

This function computes the visual position of a character from its index in the string. The returned position is in global coordinates (translation, rotation, scale and origin are applied). If index is out of range, the position of the end of the string is returned.

  • index - Index of the character

Returns: Position of the character

View source

#font=(font : Font) #

Set the text's font

The font argument refers to a font that must exist as long as the text uses it. Indeed, the text doesn't store its own copy of the font, but rather keeps a pointer to the one that you passed to this function. If the font is destroyed and the text tries to use it, the behavior is undefined.

  • font - New font

See also: font

View source

#global_bounds : FloatRect #

Get the global bounding rectangle of the entity

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the text in the global 2D world's coordinate system.

Returns: Global bounding rectangle of the entity

View source

#letter_spacing : Float32 #

Get the size of the letter spacing factor

Returns: Size of the letter spacing factor

See also: letter_spacing=

View source

#letter_spacing=(spacing_factor : Number) #

Set the letter spacing factor

The default spacing between letters is defined by the font. This factor doesn't directly apply to the existing spacing between each character, it rather adds a fixed space between them which is calculated from the font metrics and the character size. Note that factors below 1 (including negative numbers) bring characters closer to each other. By default the letter spacing factor is 1.

  • spacing_factor - New letter spacing factor

See also: letter_spacing

View source

#line_spacing : Float32 #

Get the size of the line spacing factor

Returns: Size of the line spacing factor

See also: line_spacing=

View source

#line_spacing=(spacing_factor : Number) #

Set the line spacing factor

The default spacing between lines is defined by the font. This method enables you to set a factor for the spacing between lines. By default the line spacing factor is 1.

  • spacing_factor - New line spacing factor

See also: line_spacing

View source

#local_bounds : FloatRect #

Get the local bounding rectangle of the entity

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

Returns: Local bounding rectangle of the entity

View source

#outline_color : Color #

Get the outline color of the text

Returns: Outline color of the text

See also: outline_color=

View source

#outline_color=(color : Color) #

Set the outline color of the text

By default, the text's outline color is opaque black.

  • color - New outline color of the text

See also: outline_color

View source

#outline_thickness : Float32 #

Get the outline thickness of the text

Returns: Outline thickness of the text, in pixels

See also: outline_thickness=

View source

#outline_thickness=(thickness : Number) #

Set the thickness of the text's outline

By default, the outline thickness is 0.

Be aware that using a negative value for the outline thickness will cause distorted rendering.

  • thickness - New outline thickness, in pixels

See also: outline_thickness

View source

#string : String #

Get the text's string

Returns: Text's string

See also: string=

View source

#string=(string : String) #

Set the text's string

A text's string is empty by default.

  • string - New string

See also: string

View source

#style : UInt32 #

Get the text's style

Returns: Text's style

See also: style=

View source

#style=(style : Text::Style) #

Set the text's style

You can pass a combination of one or more styles, for example SF::Text::Bold | SF::Text::Italic. The default style is SF::Text::Regular.

  • style - New style

See also: style

View source