class SF::Text
- SF::Text
- SF::Transformable
- Reference
- Object
Overview
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
Defined in:
graphics/obj.crConstructors
-
.new(string : String, font : Font, character_size : Int = 30)
Construct the text from a string, font and size
-
.new
Default constructor
Instance Method Summary
-
#character_size : Int32
Get the character size
-
#character_size=(size : Int)
Set the character size
-
#color : Color
Get the fill color of the text
-
#color=(color : Color)
Set the fill color of the text
-
#dup : Text
Returns a shallow copy of this object.
-
#fill_color : Color
Get the fill color of the text
-
#fill_color=(color : Color)
Set the fill color of the text
-
#finalize
Virtual destructor
-
#find_character_pos(index : Int) : Vector2f
Return the position of the index-th character
-
#font=(font : Font)
Set the text's font
-
#global_bounds : FloatRect
Get the global bounding rectangle of the entity
-
#letter_spacing : Float32
Get the size of the letter spacing factor
-
#letter_spacing=(spacing_factor : Number)
Set the letter spacing factor
-
#line_spacing : Float32
Get the size of the line spacing factor
-
#line_spacing=(spacing_factor : Number)
Set the line spacing factor
-
#local_bounds : FloatRect
Get the local bounding rectangle of the entity
-
#outline_color : Color
Get the outline color of the text
-
#outline_color=(color : Color)
Set the outline color of the text
-
#outline_thickness : Float32
Get the outline thickness of the text
-
#outline_thickness=(thickness : Number)
Set the thickness of the text's outline
-
#string : String
Get the text's string
-
#string=(string : String)
Set the text's string
-
#style : UInt32
Get the text's style
-
#style=(style : Text::Style)
Set the text's style
Instance methods inherited from module SF::Drawable
draw(target : RenderTarget, states : RenderStates)
draw
Instance methods inherited from class SF::Transformable
dup : Transformable
dup,
finalize
finalize,
inverse_transform : Transform
inverse_transform,
move(offset_x : Number, offset_y : Number)move(offset : Vector2 | Tuple) move, origin : Vector2f origin, origin=(origin : Vector2 | Tuple) origin=, position : Vector2f position, position=(position : Vector2 | Tuple) position=, rotate(angle : Number) rotate, rotation : Float32 rotation, rotation=(angle : Number) rotation=, scale(factor_x : Number, factor_y : Number)
scale(factor : Vector2 | Tuple)
scale : Vector2f scale, scale=(factors : Vector2 | Tuple) scale=, set_origin(x : Number, y : Number) set_origin, set_position(x : Number, y : Number) set_position, set_scale(factor_x : Number, factor_y : Number) set_scale, transform : Transform transform
Constructor methods inherited from class SF::Transformable
new
new
Constructor Detail
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
Instance Method Detail
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
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.
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.
Returns a shallow copy of this object.
This allocates a new object and copies the contents of
self
into it.
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
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
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
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
Get the size of the letter spacing factor
Returns: Size of the letter spacing factor
See also: #letter_spacing=
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
Get the size of the line spacing factor
Returns: Size of the line spacing factor
See also: #line_spacing=
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
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
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
Get the outline thickness of the text
Returns: Outline thickness of the text, in pixels
See also: #outline_thickness=
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
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