2

I have a point layer in QGIS that looks like the following picture:

label on QGIS canvas

Content of the Dicke Auf field :

BS 4/ DPM 4
Ansatzhöhe: 275.96
UK Auffüllung: 274.71
Dicke Auffüllung: 1.3
OK Sand: 274.62
Grundwasser: 271,26

Now I'd like to change the color of the different labels individually (e.g. OK Sand = red, Grundwasser = blue). Is that possible?

I already tried the editor, but I only managed to change the entire label:

if ("Dicke Auf"  >=  '0,5','cyan','red')
3
  • 2
    What software is this? QGIS? Some ESRI product? Can you add a tag for it and maybe mention in the question?
    – Spacedman
    Commented Jun 2, 2023 at 9:34
  • Thank you, how dumb to forget that. I edited the question and the tags, i work with QGis.
    – Felix
    Commented Jun 2, 2023 at 9:43
  • Be also sure to include the QGIS version you use. Otherwise, we assume you use the most recent (current) version. If you use an older version, functionalities may not be available.
    – Babel
    Commented Jun 2, 2023 at 9:56

1 Answer 1

4

Labeling in different colors is possible with html-formatted labels, introduced in QGIS 3.28. See here for some use cases.

Check the box Allow html formatting in the Text tab of label settings. Then insert a html code in the value field. Use format() function, define the styles you want using placeholders like %1, then add the text content you want to have formatted this way.

Here is an example of the syntax to use to have the first line with the content of field_1 in blue, on the next line field_2 in red:

format(
  '<span style="color:blue"> <p> %1 </p> </span> <br>
   <span style="color:red"> %2 </span>',
  field_1,
  field_2
)

or, with the same result, simply concatenating the different elements:

'<span style="color:blue"><p>'  ||  field_1  || '</p></span>'  || 
'<span style="color:red">'  ||  field_2  || '</span><br>'

enter image description here

You can also mix this to get different font sizes, bold/strong, italic etc. like this:

'<span style="color:blue"><p style="font-size: 40pt"; ><strong>'  ||  field_1  || '</strong></p></span>'  || 
'<span style="color:red"><p style="font-size: 30pt"><i>'  ||  field_2  || '</i></p></span>'  || 
'<span style="color:green"><p style="font-size: 20pt">'  ||  field3 || '</p></span>'

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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