Version

Quick search

Table Of Contents

Text Markup

New in version 1.1.0.

Changed in version 1.10.1: Added font_context, font_features and text_language (Pango only)

We provide a simple text-markup for inline text styling. The syntax look the same as the BBCode.

A tag is defined as [tag], and should have a corresponding [/tag] closing tag. For example:

[b]Hello [color=ff0000]world[/color][/b]

The following tags are available:

[b][/b]
Activate bold text
[i][/i]
Activate italic text
[u][/u]
Underlined text
[s][/s]
Strikethrough text
[font=<str>][/font]
Change the font (note: this refers to a TTF file or registered alias)
[font_context=<str>][/font_context]
Change context for the font, use string value “none” for isolated context.
[font_family=<str>][/font_family]
Font family to request for drawing. This is only valid when using a font context, and takes precedence over [font]. See kivy.uix.label.Label for details.
[font_features=<str>][/font_features]
OpenType font features, in CSS format, this is passed straight through to Pango. The effects of requesting a feature depends on loaded fonts, library versions, etc. Pango only, requires v1.38 or later.
[size=<size>][/size]
Change the font size. <size> should be an integer, optionally with a unit (i.e. 16sp)
[color=#<color>][/color]
Change the text color
[ref=<str>][/ref]
Add an interactive zone. The reference + all the word box inside the reference will be available in MarkupLabel.refs
[anchor=<str>]
Put an anchor in the text. You can get the position of your anchor within the text with MarkupLabel.anchors
[sub][/sub]
Display the text at a subscript position relative to the text before it.
[sup][/sup]
Display the text at a superscript position relative to the text before it.
[text_language=<str>][/text_language]
Language of the text, this is an RFC-3066 format language tag (as string), for example “en_US”, “zh_CN”, “fr” or “ja”. This can impact font selection, metrics and rendering. For example, the same bytes of text can look different for ur and ar languages, though both use Arabic script. Use the string ‘none’ to revert to locale detection. Pango only.

If you need to escape the markup from the current text, use kivy.utils.escape_markup().

class kivy.core.text.markup.MarkupLabel(*largs, **kwargs)[source]

Bases: kivy.core.text.LabelBase

Markup text label.

See module documentation for more informations.

anchors

Get the position of all the [anchor=...]:

{ 'anchorA': (x, y), 'anchorB': (x, y), ... }
markup

Return the text with all the markup splitted:

>>> MarkupLabel('[b]Hello world[/b]').markup
>>> ('[b]', 'Hello world', '[/b]')
refs

Get the bounding box of all the [ref=...]:

{ 'refA': ((x1, y1, x2, y2), (x1, y1, x2, y2)), ... }
render(real=False)[source]

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

shorten_post(lines, w, h, margin=2)[source]

Shortens the text to a single line according to the label options.

This function operates on a text that has already been laid out because for markup, parts of text can have different size and options.

If text_size [0] is None, the lines are returned unchanged. Otherwise, the lines are converted to a single line fitting within the constrained width, text_size [0].

Params:lines: list of LayoutLine instances describing the text. w: int, the width of the text in lines, including padding. h: int, the height of the text in lines, including padding. margin int, the additional space left on the sides. This is in addition to padding_x.
Returns:3-tuple of (xw, h, lines), where w, and h is similar to the input and contains the resulting width / height of the text, including padding. lines, is a list containing a single LayoutLine, which contains the words for the line.