6

I need to set different font sizes and different colours for my text strings, which appear in QGIS 3.x labels.

I found a solution at Setting different colors based on value when labeling using QGIS but it doesn't work in my case.

In my data attribute table, which looks like below:

enter image description here

I used the "text" column to define my label.

Now, I want to distinguish some strings, making them bigger (font size) and red instead of black.

Basing on the solution from the link above I prepared the simple function: "color24" = 1

unfortunately, along with the red color, I am getting the "1" value.

In the other case, when my function looks like:

  CASE
  WHEN "Text" like '%GW%' then color_rgb (255, 0, 0)
  END

I am getting nothing.

enter image description here

Is anyone able to help with this? I need to know how to change the font size and the font color on a categorized basis.


After the function:

   CASE When "Color24" = 1 Then color_rgb(255,0,0)
   end

and other settings, as seen in the image below...

enter image description here

gives the answer, but eventually, the result doesn't work properly. Mainly only 1 of 3 labels are changed. They shift between older settings and new one as the zoom level changes.

How can I fix it?

enter image description here

4
  • 2
    I'd go for data defined override instead of rule based labelling. Also, try removing the space between color_rgb and the (. Also, it is rather unclear what you set where.
    – Erik
    Commented Aug 13, 2020 at 14:29
  • I don't know the data defined override
    – Geographos
    Commented Aug 13, 2020 at 14:41
  • To the right of almost every field in QGIS, where you can enter a value, there's two stacked rectangles with two smaller triangles attached to them. If you click there, you may either choose a column which holds valid values to override the settings for this field, or enter an expression which does the same.
    – Erik
    Commented Aug 13, 2020 at 14:43
  • See this answer for an example: gis.stackexchange.com/questions/370986/…
    – Erik
    Commented Aug 13, 2020 at 14:45

1 Answer 1

1

Use the 'data defined override' option for 'Size' in the Labels tab of Layer Styling - you won't need rule based labelling:

In my example here, there's a basic point layer with two categories, 'house' and 'car'.

To style them differently, you click on the data-defined-override drop-down and select 'edit', then add an expression to produce the numbers for each font size:

CASE  
    WHEN "category" = 'House' THEN 14.0
    WHEN "category" = 'Car' THEN 20.0
END

Note - for simplicity you could also just have a column called "font_size" and use that as the expression!

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