'''Label ===== .. image:: images/label.png :align: right The :class:`Label` widget is for rendering text:: # hello world text l = Label(text='Hello world') # unicode text; can only display glyphs that are available in the font l = Label(text='Hello world ' + chr(2764)) # multiline text l = Label(text='Multi\\nLine') # size l = Label(text='Hello world', font_size='20sp') .. _kivy-uix-label-sizing-and-text-content: Sizing and text content --------------------------- By default, the size of :class:`Label` is not affected by :attr:`~Label.text` content and the text is not affected by the size. In order to control sizing, you must specify :attr:`~Label.text_size` to constrain the text and/or bind :attr:`~Label.size` to :attr:`~Label.texture_size` to grow with the text. For example, this label's size will be set to the text content (plus :attr:`~Label.padding`): .. code-block:: kv Label: size: self.texture_size This label's text will wrap at the specified width and be clipped to the height: .. code-block:: kv Label: text_size: cm(6), cm(4) .. note:: The :attr:`~Label.shorten` and :attr:`~Label.max_lines` attributes control how overflowing text behaves. Combine these concepts to create a Label that can grow vertically but wraps the text at a certain width: .. code-block:: kv Label: text_size: root.width, None size: self.texture_size How to have a custom background color in the label: .. code-block:: kv # Define your background color Template background_color: 1, 1, 1, 1 canvas.before: Color: rgba: root.background_color Rectangle: size: self.size pos: self.pos # Now you can simply Mix the `BackgroundColor` class with almost # any other widget... to give it a background. background_color: 0, 0, 0, 0 # Default the background color for this label # to r 0, g 0, b 0, a 0 # Use the BackgroundLabel any where in your kv code like below BackgroundLabel text: 'Hello' background_color: 1, 0, 0, 1 Text alignment and wrapping --------------------------- The :class:`Label` has :attr:`~Label.halign` and :attr:`~Label.valign` properties to control the alignment of its text. However, by default the text image (:attr:`~Label.texture`) is only just large enough to contain the characters and is positioned in the center of the Label. The valign property will have no effect and halign will only have an effect if your text has newlines; a single line of text will appear to be centered even though halign is set to left (by default). In order for the alignment properties to take effect, set the :attr:`~Label.text_size`, which specifies the size of the bounding box within which text is aligned. For instance, the following code binds this size to the size of the Label, so text will be aligned within the widget bounds. This will also automatically wrap the text of the Label to remain within this area. .. code-block:: kv Label: text_size: self.size halign: 'right' valign: 'middle' Markup text ----------- .. versionadded:: 1.1.0 You can change the style of the text using :doc:`api-kivy.core.text.markup`. The syntax is similar to the bbcode syntax but only the inline styling is allowed:: # hello world with world in bold l = Label(text='Hello [b]World[/b]', markup=True) # hello in red, world in blue l = Label(text='[color=ff3333]Hello[/color][color=3333ff]World[/color]', markup = True) If you need to escape the markup from the current text, use :func:`kivy.utils.escape_markup`:: text = 'This is an important message [1]' l = Label(text='[b]' + escape_markup(text) + '[/b]', markup=True) The following tags are available: ``[b][/b]`` Activate bold text ``[i][/i]`` Activate italic text ``[u][/u]`` Underlined text ``[s][/s]`` Strikethrough text ``[font=][/font]`` Change the font (note: this refers to a TTF file or registered alias) ``[font_context=][/font_context]`` Change context for the font, use string value "none" for isolated context (this is equivalent to `None`; if you created a font context named `'none'`, it cannot be referred to using markup) ``[font_family=][/font_family]`` Font family to request for drawing. This is only valid when using a font context, see :class:`kivy.uix.label.Label` for details. ``[font_features=][/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]`` Change the font size ``[color=#][/color]`` Change the text color ``[ref=][/ref]`` Add an interactive zone. The reference + bounding box inside the reference will be available in :attr:`Label.refs` ``[anchor=]`` Put an anchor in the text. You can get the position of your anchor within the text with :attr:`Label.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=][/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 and metrics. Use the string "None" to revert to locale detection. Pango only. If you want to render the markup text with a [ or ] or & character, you need to escape them. We created a simple syntax:: [ -> &bl; ] -> &br; & -> & Then you can write:: "[size=24]Hello &bl;World&br;[/size]" Interactive zone in text ------------------------ .. versionadded:: 1.1.0 You can now have definable "links" using text markup. The idea is to be able to detect when the user clicks on part of the text and to react. The tag ``[ref=xxx]`` is used for that. In this example, we are creating a reference on the word "World". When this word is clicked, the function ``print_it`` will be called with the name of the reference:: def print_it(instance, value): print('User clicked on', value) widget = Label(text='Hello [ref=world]World[/ref]', markup=True) widget.bind(on_ref_press=print_it) For prettier rendering, you could add a color for the reference. Replace the ``text=`` in the previous example with:: 'Hello [ref=world][color=0000ff]World[/color][/ref]' Catering for Unicode languages ------------------------------ The font kivy uses does not contain all the characters required for displaying all languages. When you use the built-in widgets, this results in a block being drawn where you expect a character. If you want to display such characters, you can chose a font that supports them and deploy it universally via kv: .. code-block:: kv