Quick search

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:


Activate bold text


Activate italic text


Underlined text


Strikethrough text


Change the font (note: this refers to a TTF file or registered alias)


Change context for the font, use string value “none” for isolated context.


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.


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.


Change the font size. <size> should be an integer, optionally with a unit (i.e. 16sp)


Change the text color


Add an interactive zone. The reference + all the word box inside the reference will be available in MarkupLabel.refs


Put an anchor in the text. You can get the position of your anchor within the text with MarkupLabel.anchors


Display the text at a subscript position relative to the text before it.


Display the text at a superscript position relative to the text before it.


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

property anchors

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

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

Return the text with all the markup split:

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

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

{ 'refA': ((x1, y1, x2, y2), (x1, y1, x2, y2)), ... }

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


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.


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.