QGIS rule-based styling for font size and color at once

labelingqgis

I would like to have two types of text in my layer.

I have one value in the data attribute table, which is "Timestamp" and its value is 0 or 1. 1 for bigger and black font, 0 for the smaller and grey font. The value I want to be displayed is "description".

I tried several approaches, which are here:

Single text label in dual font size in QGIS

How to create a label combining different font sizes or types?

Single text label in dual font size in QGIS

and finally, I used this one:

Setting different font color types from the same field in rule-based labeling in QGIS

which works fine when considered the cont color only.

enter image description here

I tried to set up two separate conditions based on the same columns in the data attribute table (timestamp for font condition and description for value displayed).

enter image description here

but it didn't work as expected.

Finally, I tried the formula:

  CASE WHEN "Timestamp" = 1 THEN
  '000000' AND 12 
   ELSE
   '808080' AND 7
   END

and text partially disappears.

What problem can be here?

Best Answer

You are misusing the rule based labeling.

The formula in the "rule" setting is used to select witch object are labelled, not how they will be labelled. So your two rule should be :

Description : Timestamp value is 1 : bigger and black font - Rule : "Timestamp" = 1

Description : Timestamp value is 0 : smaller and grey font - Rule : "Timestamp" = 0

(It could be advisable to set a third "ELSE" rule if you can't guarantee that the timestamp will always be filled with either 0 or 1)

Then you set how each rule are labeled the exact same way you would while using single labels . Each rule are totally independent on how they are labelled and can use any setting regardless on setting on other rules. It's the same as if you where labeling two different layer.

enter image description here

enter image description here

enter image description here

If you want to use data defined override you should select single label, set all labeling option the usual way except for the font size and font color where you should select the edit option by clicking on the symbol next right to the font and symbol selector and use an expression to set the relevant setting (for font size you may use the CASE WHEN "Timestamp" = 1 THEN 10 ELSE 5 END expression suggested by @Erik)

enter image description here

Note that when using data defined override you need to input an expression for each setting you want to individualise while rule labeling require that you create a rule for each kind of label you need.

So as a rule of thumb : for creating a few distinct label type (as in your case two distinct label) it's better to use rule labeling. While if you want to get lot of similar label (like a label colored accordingly to a field value) it's better to use data defined override. Of course for overly complex labeling you may mix the two approach....