Mixed Format Labels in QGIS — coming soon!
For the QGIS 3.28 release North Road had the exciting opportunity to add some much desired new functionality to QGIS: the ability to mix font formatting and styles within a single map label! This enhancement was funded by the Swiss QGIS User Group, and offers a whole range of new cartographic possibilities for all QGIS users. Read on for more details on how this can impact your mapping…
QGIS has long supported the ability to combine different values inside a label for a feature, via the use of custom QGIS expressions. For example, the map below labels features from a rail station layer with the expression
"NAME" || '\n' || "PATRONAGE"
This expression concatenates the field values inside the “NAME” field with their “PATRONAGE” values, using a new line character to separate the two values. The end result looks something like this:
While custom expressions are extremely flexible and offer an almost unlimited range of possibilities to tweak label content, the limitation has been that all of this label text will always be displayed using a single font, font size, and font style. For example, there was no capacity to show the station names in a larger, bold font, and the patronage values in a smaller size.
That’s all changing thanks to the funding from the QGIS Swiss User Group! Now, in QGIS 3.28, 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. Let’s revisit the map shown above and flex these new cartographic muscles…
The first thing we need to do is enable the HTML formatting tags for our labels by checking the “Allow HTML formatting” option in the text properties:
With this option enabled, we can now use a range of HTML and CSS tags to format individual parts of our label. For instance, “<b>bold text</b>” will show the text between the <b> and </b> tags in a bold font. By revising our previous labelling expression we can now get the station names to render in a bold font. Here’s the revised expression:
'<b>' || "NAME" || '</b><p>' || "PATRONAGE"
There’s one other important change we’ve made in the above expression. When using HTML formatting for labels, we need to replace the ‘\n’ newline character with the HTML new paragraph tag “<p>”. Otherwise, our expression is very similar to the previous one, with the exception that we’re wrapping the “NAME” field in <b>…</b> tags.
Here’s how it looks:
That’s already a dramatic improvement in the readability of the map! Let’s try refining it further by dropping down the font size of the patronage values. We can do that by inserting some CSS into the label string, specifically the “font-size” property. Here’s our new expression:
'<b>' || "NAME" || '</b><p><span style="font-size: 10pt">' || "PATRONAGE" || '</span>'
It’s so beautiful now! (Go on, scroll back up to our original map, we dare you to compare!).
So which HTML/CSS formatting options are supported? For QGIS 3.28, these options are:
- Font family, via the CSS font-family tag. E.g. <span style=”font-family: Comic Sans MS”>…</span>
- Font size, via the CSS font-size tag. E.g. <span style=”font-size: 30pt”>…</span>
- Font weights, via the HTML <b> tag or CSS font-weight tag
- Italic, via the HTML <i> tag or CSS “font-style: italic”
- Font color, via the CSS “color: #rrggbb” tag
- Underline, via the HTML <u> tag or CSS “text-decoration: underline” tag
- Strikethrough, via the HTML <s> tag or CSS “text-decoration: line-through” tag
- Overline, via the CSS “text-decoration: overline” tag
Oh, one last thing we nearly forgot to mention: all these wonderful styling options also apply perfectly to curved label text!
Our thanks go out to the members of the Swiss QGIS User Group for funding this feature! It’s an exciting addition to QGIS and can offer new ways to dramatically improve your mapping creations.
Got any QGIS needs you’d like to discuss? Or want to see additional formatting options added in a future QGIS release? Just contact our friendly team to discuss your needs — we’d love to assist!