Text

sfml.graphics.Text
See theText companion object
trait Text(using ctor: ctor) extends Transformable, Drawable

Graphical text that can be drawn to a render target.

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 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.

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

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

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

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

val window: RenderWindow = ???

// Declare and load a font
val font = Font()
font.loadFromFile("arial.ttf")

// Create a text
val text = Text("hello", font)
text.characterSize = 30;

text.fillColor = Color.Red()

// Draw it
window.draw(text)

Attributes

See also
Companion
object
Graph
Supertypes
trait Drawable
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def characterSize: Int

Get the character size.

Get the character size.

Attributes

Returns

Size of the characters, in pixels

See also
def characterSize_=(size: Int): Unit

Set the character 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.

Value parameters

size

New character size, in pixels

Attributes

See also
def color: Color

Get the fill color of the text.

Get the fill color of the text.

Attributes

Returns

Fill color of the text

See also
def color_=(color: Color): Unit

Set the fill color of the text.

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.

Value parameters

color

New fill color of the text

Attributes

See also
override def draw(target: RenderTarget, states: RenderStates): Unit

Draw the object to a render target.

Draw the object to a render target.

This is a function that has to be implemented by the derived class to define how the drawable should be drawn.

Value parameters

states

Current render states

target

Render target to draw to

Attributes

Definition Classes

Get the fill color of the text.

Get the fill color of the text.

Attributes

Returns

Fill color of the text

See also
def fillColor_=(color: Color): Unit

Set the fill color of the text.

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.

Value parameters

color

New fill color of the text

Attributes

See also
def font: Option[Immutable[Font]]
def font_=(font: Font): Unit

Set the text's 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.

Value parameters

font

New font

Attributes

See also
def globalBounds: Rect[Float]

Get the global bounding rectangle of the entity.

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.

Attributes

Returns

Global bounding rectangle of the entity

def letterSpacing: Float

Get the size of the letter spacing factor.

Get the size of the letter spacing factor.

Attributes

Returns

Size of the letter spacing factor

See also
def letterSpacing_=(spacingFactor: Float): Unit

Set the letter spacing factor.

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.

Value parameters

spacingFactor

New letter spacing factor

Attributes

See also
def lineSpacing: Float

Get the size of the line spacing factor.

Get the size of the line spacing factor.

Attributes

Returns

Size of the line spacing factor

See also
def lineSpacing_=(spacingFactor: Float): Unit

Set the line spacing factor.

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.

Value parameters

spacingFactor

New line spacing factor

Attributes

See also
def localBounds: Rect[Float]

Get the local bounding rectangle of the entity.

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.

Attributes

Returns

Local bounding rectangle of the entity

Get the outline color of the text.

Get the outline color of the text.

Attributes

Returns

Outline color of the text

See also
def outlineColor_=(color: Color): Unit

Set the outline color of the text.

Set the outline color of the text.

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

Value parameters

color

New outline color of the text

Attributes

See also
def outlineThickness: Float

Get the outline thickness of the text.

Get the outline thickness of the text.

Attributes

Returns

Outline thickness of the text, in pixels

See also
def outlineThickness_=(thickness: Float): Unit

Set the thickness of the text's outline.

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.

Value parameters

thickness

New outline thickness, in pixels

Attributes

See also
def string: Unit
def string_=(string: String): Unit

Set the text's string.

Set the text's string.

A text's string is empty by default.

Value parameters

string

New string

Attributes

Inherited methods

get the inverse of the combined transform of the object

get the inverse of the combined transform of the object

Attributes

Returns

Inverse of the combined transformations applied to the object

See also
Inherited from:
Transformable
def move(offset: Vector2[Float]): Unit

Move the object by a given offset.

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:

import sfml.system.Vector2

val obj: Transformable = ???
val offset: Vector2[Float] = ???

obj.position = obj.position + offset

Value parameters

offset

Offset

Attributes

See also
Inherited from:
Transformable
def move(offsetX: Float, offsetY: Float): Unit

Move the object by a given offset.

Move the object by a given offset.

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

import sfml.system.Vector2

val obj: Transformable = ???
val offsetX, offsetY: Float = ???

val pos: Vector2[Float] = obj.position
obj.position = (pos.x + offsetX, pos.y + offsetY)

Value parameters

offsetX

X offset

offsetY

Y offset

Attributes

See also
Inherited from:
Transformable
def origin: Vector2[Float]

get the local origin of the object

get the local origin of the object

Attributes

Returns

Current origin

See also
Inherited from:
Transformable
def origin_=(origin: Vector2[Float]): Unit

set the local origin of the object

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).

Value parameters

origin

New origin

Attributes

See also
Inherited from:
Transformable
def origin_=(x: Float, y: Float): Unit

set the local origin of the object

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).

Value parameters

x

X coordinate of the new origin

y

Y coordinate of the new origin

Attributes

See also
Inherited from:
Transformable
def position: Vector2[Float]

get the position of the object

get the position of the object

Attributes

Returns

Current position

See also
Inherited from:
Transformable
def position_=(position: Vector2[Float]): Unit

set the position of the object

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).

Value parameters

position

New position

Attributes

See also
Inherited from:
Transformable
def position_=(x: Float, y: Float): Unit

set the position of the object

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).

Value parameters

x

X coordinate of the new position

y

Y coordinate of the new position

Attributes

See also
Inherited from:
Transformable
def rotate(angle: Float): Unit

Rotate the object.

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:

val obj: Transformable = ???
val angle: Float = ???

obj.rotation = obj.rotation + angle

Value parameters

angle

Angle of rotation, in degrees

Attributes

Inherited from:
Transformable
def rotation: Float

get the orientation of the object

get the orientation of the object

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

Attributes

Returns

Current rotation, in degrees

See also
Inherited from:
Transformable
def rotation_=(angle: Float): Unit

set the orientation of the object

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.

Value parameters

angle

New rotation, in degrees

Attributes

See also
Inherited from:
Transformable
def scale: Vector2[Float]

get the current scale of the object

get the current scale of the object

Attributes

Returns

Current scale factors

See also
Inherited from:
Transformable
def scale(factor: Vector2[Float]): Unit

Scale the object.

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:

import sfml.system.Vector2

val obj: Transformable = ???
val factor: Vector2[Float] = ???

val scale: Vector2[Float] = obj.scale
obj.scale = (scale.x * factor.x, scale.y * factor.y)

Value parameters

factor

Scale factors

Attributes

See also
Inherited from:
Transformable
def scale(factorX: Float, factorY: Float): Unit

Scale the object.

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:

import sfml.system.Vector2

val obj: Transformable = ???
val factorX, factorY: Float = ???

val scale: Vector2[Float] = obj.scale
obj.scale = (scale.x + factorX, scale.y + factorY)

Value parameters

factorX

Horizontal scale factor

factorY

Vertical scale factor

Attributes

See also
Inherited from:
Transformable
def scale_=(factors: Vector2[Float]): Unit

set the scale factors of the object

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).

Value parameters

factors

New scale factors

Attributes

See also
Inherited from:
Transformable
def scale_=(x: Float, y: Float): Unit

set the scale factors of the object

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).

Value parameters

factorX

New horizontal scale factor

factorY

New vertical scale factor

Attributes

See also
Inherited from:
Transformable

get the combined transform of the object

get the combined transform of the object

Attributes

Returns

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

See also
Inherited from:
Transformable