SFML 3.0.2
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
sf::Text Class Reference
Graphics module

Graphical text that can be drawn to a render target. More...

`#include <SFML/Graphics/Text.hpp>`

Inheritance diagram for sf::Text:

## Public Types

enum | Style { Regular = 0 , Bold = 1 << 0 , Italic = 1 << 1 , Underlined = 1 << 2 , StrikeThrough = 1 << 3 }
| Enumeration of the string drawing styles. More...

## Public Member Functions

| Text (const Font &font, String string="", unsigned int characterSize=30)
| Construct the text from a string, font and size.
| Text (const Font &&font, String string="", unsigned int characterSize=30)=delete
| Disallow construction from a temporary font.
void | setString (const String &string)
| Set the text's string.
void | setFont (const Font &font)
| Set the text's font.
void | setFont (const Font &&font)=delete
| Disallow setting from a temporary font.
void | setCharacterSize (unsigned int size)
| Set the character size.
void | setLineSpacing (float spacingFactor)
| Set the line spacing factor.
void | setLetterSpacing (float spacingFactor)
| Set the letter spacing factor.
void | setStyle (std::uint32_t style)
| Set the text's style.
void | setFillColor (Color color)
| Set the fill color of the text.
void | setOutlineColor (Color color)
| Set the outline color of the text.
void | setOutlineThickness (float thickness)
| Set the thickness of the text's outline.
const String & | getString () const
| Get the text's string.
const Font & | getFont () const
| Get the text's font.
unsigned int | getCharacterSize () const
| Get the character size.
float | getLetterSpacing () const
| Get the size of the letter spacing factor.
float | getLineSpacing () const
| Get the size of the line spacing factor.
std::uint32_t | getStyle () const
| Get the text's style.
Color | getFillColor () const
| Get the fill color of the text.
Color | getOutlineColor () const
| Get the outline color of the text.
float | getOutlineThickness () const
| Get the outline thickness of the text.
Vector2f | findCharacterPos (std::size_t index) const
| Return the position of the index-th character.
FloatRect | getLocalBounds () const
| Get the local bounding rectangle of the entity.
FloatRect | getGlobalBounds () const
| Get the global bounding rectangle of the entity.
void | setPosition (Vector2f position)
| set the position of the object
void | setRotation (Angle angle)
| set the orientation of the object
void | setScale (Vector2f factors)
| set the scale factors of the object
void | setOrigin (Vector2f origin)
| set the local origin of the object
Vector2f | getPosition () const
| get the position of the object
Angle | getRotation () const
| get the orientation of the object
Vector2f | getScale () const
| get the current scale of the object
Vector2f | getOrigin () const
| get the local origin of the object
void | move (Vector2f offset)
| Move the object by a given offset.
void | rotate (Angle angle)
| Rotate the object.
void | scale (Vector2f factor)
| Scale the object.
const Transform & | getTransform () const
| get the combined transform of the object
const Transform & | getInverseTransform () const
| get the inverse of the combined transform of the object

## Detailed Description

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:

// Open a font
const sf::Font font("arial.ttf");
// Create a text
sf::Text text(font, "hello");
text.setCharacterSize(30);
text.setStyle(sf::Text::Bold);
text.setFillColor(sf::Color::Red);
// Draw it
window.draw(text);
sf::Color::Red
static const Color Red
Red predefined color.
Definition Color.hpp:84
sf::Font
Class for loading and manipulating character fonts.
Definition Font.hpp:58
sf::Text
Graphical text that can be drawn to a render target.
Definition Text.hpp:57
sf::Text::Bold
@ Bold
Bold characters.
Definition Text.hpp:66

See also
  sf::Font, sf::Transformable

Definition at line 56 of file Text.hpp.

## Member Enumeration Documentation

## ◆ Style

enum sf::Text::Style

Enumeration of the string drawing styles.

Enumerator
Regular | Regular characters, no style.
Bold | Bold characters.
Italic | Italic characters.
Underlined | Underlined characters.
StrikeThrough | Strike through characters.

Definition at line 63 of file Text.hpp.

## Constructor & Destructor Documentation

## ◆ Text() [1/2]

sf::Text::Text | ( | const Font & | font,
|  | String | string = "",
|  | unsigned int | characterSize = 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.

Parameters

  string | Text assigned to the string
  font | Font used to draw the string
  characterSize | Base size of characters, in pixels

## ◆ Text() [2/2]

sf::Text::Text | ( | const Font && | font,
|  | String | string = "",
|  | unsigned int | characterSize = 30 )

delete

Disallow construction from a temporary font.

## Member Function Documentation

## ◆ findCharacterPos()

Vector2f sf::Text::findCharacterPos | ( | std::size_t | index | ) | const

nodiscard

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.

Parameters

  index | Index of the character

Returns
  Position of the character

## ◆ getCharacterSize()

unsigned int sf::Text::getCharacterSize | ( |  | ) | const

nodiscard

Get the character size.

Returns
  Size of the characters, in pixels

See also
  setCharacterSize

## ◆ getFillColor()

Color sf::Text::getFillColor | ( |  | ) | const

nodiscard

Get the fill color of the text.

Returns
  Fill color of the text

See also
  setFillColor

## ◆ getFont()

const Font & sf::Text::getFont | ( |  | ) | const

nodiscard

Get the text's font.

The returned reference is const, which means that you cannot modify the font when you get it from this function.

Returns
  Reference to the text's font

See also
  setFont

## ◆ getGlobalBounds()

FloatRect sf::Text::getGlobalBounds | ( |  | ) | const

nodiscard

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

## ◆ getInverseTransform()

const Transform & sf::Transformable::getInverseTransform | ( |  | ) | const

nodiscard inherited

get the inverse of the combined transform of the object

Returns
  Inverse of the combined transformations applied to the object

See also
  getTransform

## ◆ getLetterSpacing()

float sf::Text::getLetterSpacing | ( |  | ) | const

nodiscard

Get the size of the letter spacing factor.

Returns
  Size of the letter spacing factor

See also
  setLetterSpacing

## ◆ getLineSpacing()

float sf::Text::getLineSpacing | ( |  | ) | const

nodiscard

Get the size of the line spacing factor.

Returns
  Size of the line spacing factor

See also
  setLineSpacing

## ◆ getLocalBounds()

FloatRect sf::Text::getLocalBounds | ( |  | ) | const

nodiscard

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

## ◆ getOrigin()

Vector2f sf::Transformable::getOrigin | ( |  | ) | const

nodiscard inherited

get the local origin of the object

Returns
  Current origin

See also
  setOrigin

## ◆ getOutlineColor()

Color sf::Text::getOutlineColor | ( |  | ) | const

nodiscard

Get the outline color of the text.

Returns
  Outline color of the text

See also
  setOutlineColor

## ◆ getOutlineThickness()

float sf::Text::getOutlineThickness | ( |  | ) | const

nodiscard

Get the outline thickness of the text.

Returns
  Outline thickness of the text, in pixels

See also
  setOutlineThickness

## ◆ getPosition()

Vector2f sf::Transformable::getPosition | ( |  | ) | const

nodiscard inherited

get the position of the object

Returns
  Current position

See also
  setPosition

## ◆ getRotation()

Angle sf::Transformable::getRotation | ( |  | ) | const

nodiscard inherited

get the orientation of the object

The rotation is always in the range [0, 360].

Returns
  Current rotation

See also
  setRotation

## ◆ getScale()

Vector2f sf::Transformable::getScale | ( |  | ) | const

nodiscard inherited

get the current scale of the object

Returns
  Current scale factors

See also
  setScale

## ◆ getString()

const String & sf::Text::getString | ( |  | ) | const

nodiscard

Get the text's string.

The returned string is a sf::String, which can automatically be converted to standard string types. So, the following lines of code are all valid:

sf::String s1 = text.getString();
std::string s2 = text.getString();
std::wstring s3 = text.getString();
sf::String
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89

Returns
  Text's string

See also
  setString

## ◆ getStyle()

std::uint32_t sf::Text::getStyle | ( |  | ) | const

nodiscard

Get the text's style.

Returns
  Text's style

See also
  setStyle

## ◆ getTransform()

const Transform & sf::Transformable::getTransform | ( |  | ) | const

nodiscard inherited

get the combined transform of the object

Returns
  Transform combining the position/rotation/scale/origin of the object

See also
  getInverseTransform

## ◆ move()

void sf::Transformable::move | ( | Vector2f | offset | ) |

inherited

Move the object by a given offset.

This function adds to the current position of the object, unlike setPosition which overwrites it. Thus, it is equivalent to the following code:

object.setPosition(object.getPosition() + offset);
sf::Transformable::getPosition
Vector2f getPosition() const
get the position of the object

Parameters

  offset | Offset

See also
  setPosition

## ◆ rotate()

void sf::Transformable::rotate | ( | Angle | angle | ) |

inherited

Rotate the object.

This function adds to the current rotation of the object, unlike setRotation which overwrites it. Thus, it is equivalent to the following code:

object.setRotation(object.getRotation() + angle);
sf::Transformable::getRotation
Angle getRotation() const
get the orientation of the object

Parameters

  angle | Angle of rotation

## ◆ scale()

void sf::Transformable::scale | ( | Vector2f | factor | ) |

inherited

Scale the object.

This function multiplies the current scale of the object, unlike setScale which overwrites it. Thus, it is equivalent to the following code:

sf::Vector2f scale = object.getScale();
object.setScale(scale.x * factor.x, scale.y * factor.y);
sf::Transformable::scale
void scale(Vector2f factor)
Scale the object.
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:210

Parameters

  factor | Scale factors

See also
  setScale

## ◆ setCharacterSize()

void sf::Text::setCharacterSize | ( | unsigned int | size | ) |

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.

Parameters

  size | New character size, in pixels

See also
  getCharacterSize

## ◆ setFillColor()

void sf::Text::setFillColor | ( | 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.

Parameters

  color | New fill color of the text

See also
  getFillColor

## ◆ setFont() [1/2]

void sf::Text::setFont | ( | const Font && | font | ) |

delete

Disallow setting from a temporary font.

## ◆ setFont() [2/2]

void sf::Text::setFont | ( | const 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.

Parameters

  font | New font

See also
  getFont

## ◆ setLetterSpacing()

void sf::Text::setLetterSpacing | ( | float | spacingFactor | ) |

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.

Parameters

  spacingFactor | New letter spacing factor

See also
  getLetterSpacing

## ◆ setLineSpacing()

void sf::Text::setLineSpacing | ( | float | spacingFactor | ) |

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.

Parameters

  spacingFactor | New line spacing factor

See also
  getLineSpacing

## ◆ setOrigin()

void sf::Transformable::setOrigin | ( | Vector2f | origin | ) |

inherited

set the local origin of the object

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a transformable object is (0, 0).

Parameters

  origin | New origin

See also
  getOrigin

## ◆ setOutlineColor()

void sf::Text::setOutlineColor | ( | Color | color | ) |

Set the outline color of the text.

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

Parameters

  color | New outline color of the text

See also
  getOutlineColor

## ◆ setOutlineThickness()

void sf::Text::setOutlineThickness | ( | float | 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.

Parameters

  thickness | New outline thickness, in pixels

See also
  getOutlineThickness

## ◆ setPosition()

void sf::Transformable::setPosition | ( | Vector2f | position | ) |

inherited

set the position of the object

This function completely overwrites the previous position. See the move function to apply an offset based on the previous position instead. The default position of a transformable object is (0, 0).

Parameters

  position | New position

See also
  move, getPosition

## ◆ setRotation()

void sf::Transformable::setRotation | ( | Angle | angle | ) |

inherited

set the orientation of the object

This function completely overwrites the previous rotation. See the rotate function to add an angle based on the previous rotation instead. The default rotation of a transformable object is 0.

Parameters

  angle | New rotation

See also
  rotate, getRotation

## ◆ setScale()

void sf::Transformable::setScale | ( | Vector2f | factors | ) |

inherited

set the scale factors of the object

This function completely overwrites the previous scale. See the scale function to add a factor based on the previous scale instead. The default scale of a transformable object is (1, 1).

Parameters

  factors | New scale factors

See also
  scale, getScale

## ◆ setString()

void sf::Text::setString | ( | const String & | string | ) |

Set the text's string.

The string argument is a sf::String, which can automatically be constructed from standard string types. So, the following calls are all valid:

text.setString("hello");
text.setString(L"hello");
text.setString(std::string("hello"));
text.setString(std::wstring(L"hello"));

A text's string is empty by default.

Parameters

  string | New string

See also
  getString

## ◆ setStyle()

void sf::Text::setStyle | ( | std::uint32_t | 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.

Parameters

  style | New style

See also
  getStyle

The documentation for this class was generated from the following file:

- Text.hpp