5

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?

6
  • You can't have formatting change within a single label, but several lines and fields within one label are possible. I suggest you specify your question. – Erik Dec 3 '20 at 13:51
  • Ok, I see I can use \n to get a new line when I enter the string in the expression editor with format(). But how can I change font size for a certain field? Or underline only the first line? – Mario Dec 3 '20 at 13:54
  • 1
    As I said, you can't change formatting within a single label. – Erik Dec 3 '20 at 13:55
  • Since QGIS 3.14, only color HTML tags works : qgis.org/en/site/forusers/visualchangelog314/… – J. Monticolo Dec 3 '20 at 14:03
  • Nice, but size and other HTML tags would be very useful as well. Are there plans to develop this? – Mario Dec 3 '20 at 14:28
11

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>'

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.