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.label 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='DroidSans', bold=False, italic=False, halign='left', valign='bottom', shorten=False, text_size=None, mipmap=False, color=None, line_height=1.0, strip=False, shorten_from='center', split_str=' ', unicode_errors='replace', **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.
- 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.
- unicode_errors : str, defaults to ‘replace’
How to handle unicode decode errors. Can be ‘strict’, ‘replace’ or ‘ignore’.
Changed in version 1.9.0: strip, 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.
- 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.
- label¶
Get/Set 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. Retruns : 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.