Change font size of label with data defined expression in QGIS?

Firstly, go on the Layer Properties > Labels > Text window.

Then, edit the expression for the text size (I marked it in the image below):

enter image description here

In the expression field, simply write the name of the field which stores the sizes for the fonts of the labels. In my case, the field was called font_height, so I wrote (remember to use these " "):

enter image description here

Finally, click on the Apply button and this will be the result:

enter image description here


In @mgri 's answer "font_height" will need to give a valid value for the size field (i.e. a number or number-like string). If it is a number you can simply use "font_height" as the expression. If not you will need to do something with the value. If "font_height" had values of 'big', 'medium' and 'small' you would need to use some conditional logic:

CASE
    WHEN "font_height" = 'big' THEN 14
    WHEN "font_height" = 'medium' THEN 10
    WHEN "font_height" = 'small' THEN 8
END

Alternatively, if "font_height" values were something like '14pt' you would need to something like:

replace("font_height", 'pt', '')

Just to add to mgri's answer, you can use CASE WHEN at the same location:

CASE WHEN [column-A] = [something] THEN 3
     WHEN [column-A] = [something else] THEN 4
     ELSE 5
END

Tags:

Labeling

Qgis