For complex labels, it would be useful to be able to use HTML formatting when different fields should be shown, on multiple lines and with different font sizes.
Is this possible with QGIS? Or is there a workaround?
For complex labels, it would be useful to be able to use HTML formatting when different fields should be shown, on multiple lines and with different font sizes.
Is this possible with QGIS? Or is there a workaround?
As others have already mentioned, HTML formatting for labels is currently limited to colors. But with a very nice undocumented feature we can build dynamic labels, using label backgrounds with inline SVG expressions. Just as good as HTML formatting and best of all, we can even embed raster images (i.e. PNG) that are stored in a BLOB column (i.e. "image") of a Geopackage table (i.e. "images"). In the second picture I am highlighting the line in the expression, where the image names are constructed from the polygon attribute "bl" (county name: i.e. "Salzburg") and matched with the column "name". Take care that, due to caching issues, you will see cloud symbols instead of SVG symbols when printing the first time:
'data:image/svg+xml;utf8,<svg width="100" height="100" version="1.1" viewBox="-25 -25 50 50" xmlns="http://www.w3.org/2000/svg">
<image x="-'||format_number(to_int(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'width')) / 14,0)||'" y="-'||format_number(to_int(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'height')) / 15,0)||'" height="'||
format_number(to_int(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'height')) / 7,5)||'" width="'||
format_number(to_int(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'width')) / 7,5)||'" transform="translate(-'||
format_number(to_int(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'width')) / 15,0)||',-'||
format_number(to_int(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'height')) / 15,0)||'"
xlink:href="data:image/png;base64,'|| to_base64(attribute(get_feature( 'images','name',concat('Wappen_', "bl" )),'image'))
||'" preserveAspectRatio="xMidYMid meet"/>
<g fill="#FFFFFF" fill-opacity=".7" stroke="#000000">
<circle r="24"/>
<path d="m-23-7.5h46" fill="none" stroke="#000000" stroke-width=".8px"/>
<path d="m-23 7.5h46" fill="none" stroke="#000000" stroke-width=".8px"/>
</g>
<g alignment-baseline="middle" stroke-width=".25" text-anchor="middle">
<text y="-10" font-size="13px">AT</text>
<text y="3" fill="#FF0000" font-family="Impact" fill="#55FF55" stroke="#000000" stroke-width="4px" font-size="8px">'||replace("bl",'reich','r.')||'</text>
<text y="14.5" font-size="7px">'||"area"||'</text>
</g>
</svg>'
If we encounter some cloud symbols instead of the SVG images, there is a timing problem which can be solved as described here:
Image size limitation in QGIS print layout using HTML code

The next QGIS release (QGIS 3.28, to be released) comes with a new feature: Mixed Format Labels in QGIS. It will be released in a month (Oct. 21, 2022) and will allow html formatting:
users can combine label text with HTML formatting tags in order to customise exactly how each individual word or character in the label is drawn.
See: Mixed Format Labels in QGIS — coming soon! September 9, 2022 North Road
As of now, you can test the nightly release (QGIS 3.27, testing version) to see how it works - however, as this is not a stable version, it should not be used for productive work.
See here for some experiments with this new feature.
christoph's answer worked great for me, but I want to mention another workaround for people who come to this page just wanting to apply multiple kinds of simple formatting to a single-line label (e.g., bold one part, italicize another part, etc).
If you export from layout composer to SVG while keeping labels as text objects (not as paths), you can edit them as text and style them accordingly in vector art software like Illustrator or Inkscape. The caveat is you'll have to use one of the layers as a clipping mask, since QGIS's svg export system sometimes suffers from clipping errors.
Edit: I forgot to mention - this only works if your labels do not use a text buffer. The only way to export a text buffer properly is to export all text as paths (see QGIS issue #35854)
\nto get a new line when I enter the string in the expression editor withformat(). But how can I change font size for a certain field? Or underline only the first line? – Mario Dec 03 '20 at 13:54