Version

Quick search

Table Of Contents

Text

An abstraction of text creation. Depending of the selected backend, the accuracy of text rendering may vary.

Changed in version 1.5.0: LabelBase.line_height added.

Changed in version 1.0.7: The LabelBase does not generate any texture if the text has a width <= 1.

This is the backend layer for getting text out of different text providers, you should only be using this directly if your needs aren’t fulfilled by the Label.

Usage example:

from kivy.core.text import Label as CoreLabel

...
...
my_label = CoreLabel()
my_label.text = 'hello'
# the label is usually not drawn until needed, so force it to draw.
my_label.refresh()
# Now access the texture of the label and use it wherever and
# however you may please.
hello_texture = my_label.texture
class kivy.core.text.LabelBase(text='', font_size=12, font_name=None, bold=False, italic=False, underline=False, strikethrough=False, halign='left', valign='bottom', shorten=False, text_size=None, mipmap=False, color=None, line_height=1.0, strip=False, strip_reflow=True, shorten_from='center', split_str=' ', unicode_errors='replace', font_hinting='normal', font_kerning=True, font_blended=True, outline_width=None, outline_color=None, **kwargs)[source]

Bases: builtins.object

Core text label. This is the abstract class used by different backends to render text.

Warning

The core text label can’t be changed at runtime. You must recreate one.

Parameters:
font_size: int, defaults to 12

Font size of the text

font_name: str, defaults to DEFAULT_FONT

Font name of the text

bold: bool, defaults to False

Activate “bold” text style

italic: bool, defaults to False

Activate “italic” text style

text_size: tuple, defaults to (None, None)

Add constraint to render the text (inside a bounding box). If no size is given, the label size will be set to the text size.

padding: float, defaults to None

If it’s a float, it will set padding_x and padding_y

padding_x: float, defaults to 0.0

Left/right padding

padding_y: float, defaults to 0.0

Top/bottom padding

halign: str, defaults to “left”

Horizontal text alignment inside the bounding box

valign: str, defaults to “bottom”

Vertical text alignment inside the bounding box

shorten: bool, defaults to False

Indicate whether the label should attempt to shorten its textual contents as much as possible if a size is given. Setting this to True without an appropriately set size will lead to unexpected results.

shorten_from: str, defaults to center

The side from which we should shorten the text from, can be left, right, or center. E.g. if left, the ellipsis will appear towards the left side and it will display as much text starting from the right as possible.

split_str: string, defaults to ‘ ‘ (space)

The string to use to split the words by when shortening. If empty, we can split after every character filling up the line as much as possible.

max_lines: int, defaults to 0 (unlimited)

If set, this indicate how maximum line are allowed to render the text. Works only if a limitation on text_size is set.

mipmap: bool, defaults to False

Create a mipmap for the texture

strip: bool, defaults to False

Whether each row of text has its leading and trailing spaces stripped. If halign is justify it is implicitly True.

strip_reflow: bool, defaults to True

Whether text that has been reflowed into a second line should be stripped, even if strip is False. This is only in effect when size_hint_x is not None, because otherwise lines are never split.

unicode_errors: str, defaults to ‘replace’

How to handle unicode decode errors. Can be ‘strict’, ‘replace’ or ‘ignore’.

outline_width: int, defaults to None

Width in pixels for the outline.

outline_color: tuple, defaults to (0, 0, 0)

Color of the outline.

Changed in version 1.10.0: outline_width and outline_color were added.

Changed in version 1.9.0: strip, strip_reflow, shorten_from, split_str, and unicode_errors were added.

Changed in version 1.9.0: padding_x and padding_y has been fixed to work as expected. In the past, the text was padded by the negative of their values.

Changed in version 1.8.0: max_lines parameters has been added.

Changed in version 1.0.8: size have been deprecated and replaced with text_size.

Changed in version 1.0.7: The valign is now respected. This wasn’t the case previously so you might have an issue in your application if you have not considered this.

content_height

Return the content height; i.e. the height of the text without any padding.

content_size

Return the content size (width, height)

content_width

Return the content width; i.e. the width of the text without any padding.

fontid

Return a unique id for all font parameters

get_cached_extents()[source]

Returns a cached version of the get_extents() function.

>>> func = self._get_cached_extents()
>>> func
<built-in method size of pygame.font.Font object at 0x01E45650>
>>> func('a line')
(36, 18)

Warning

This method returns a size measuring function that is valid for the font settings used at the time get_cached_extents() was called. Any change in the font settings will render the returned function incorrect. You should only use this if you know what you’re doing.

New in version 1.9.0.

get_extents(text)[source]

Return a tuple (width, height) indicating the size of the specified text

static get_system_fonts_dir()[source]

Return the directories used by the system for fonts.

label

Get/Set the text

refresh()[source]

Force re-rendering of the text

static register(name, fn_regular, fn_italic=None, fn_bold=None, fn_bolditalic=None)[source]

Register an alias for a Font.

New in version 1.1.0.

If you’re using a ttf directly, you might not be able to use the bold/italic properties of the ttf version. If the font is delivered in multiple files (one regular, one italic and one bold), then you need to register these files and use the alias instead.

All the fn_regular/fn_italic/fn_bold parameters are resolved with kivy.resources.resource_find(). If fn_italic/fn_bold are None, fn_regular will be used instead.

render(real=False)[source]

Return a tuple (width, height) to create the image with the user constraints. (width, height) includes the padding.

shorten(text, margin=2)[source]

Shortens the text to fit into a single line by the width specified by text_size [0]. If text_size [0] is None, it returns text text unchanged.

split_str and shorten_from determines how the text is shortened.

Params:text str, the text to be shortened. margin int, the amount of space to leave between the margins and the text. This is in addition to padding_x.
Returns:the text shortened to fit into a single line.
text

Get/Set the text

text_size

Get/set the (width, height) of the ‘ ‘contrained rendering box

usersize

(deprecated) Use text_size instead.