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.

class kivy.core.text.LabelBase(text='', font_size=12, font_name='DroidSans', bold=False, italic=False, halign='left', valign='bottom', shorten=False, text_size=None, mipmap=False, color=None, line_height=1.0, **kwargs)[source]

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

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

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[source]

Return the content height

content_size[source]

Return the content size (width, height)

content_width[source]

Return the content width

fontid[source]

Return a unique id for all font parameters

get_extents(text)[source]

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

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.

2 differents methods are used:
  • if the user does not set the width, split the line and calculate max width + height
  • if the user sets a width, blit per glyph
text

Get/Set the text

text_size

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

usersize

(deprecated) Use text_size instead.