I am trying to change the font size of the feature labels based on attributes from a field. I can't find an example of how to do this.

I believe I can do this by setting up a data defined expression in the Labels>Text>Size expression but I am not sure how to formulate the actual expression.

I am looking for an example of how this expression would be set up.


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:

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

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

replace("font_height", 'pt', '')
  • +1 Good point, I think that Caitlin knows all the possible situations by now! =) – mgri Feb 14 '17 at 9:42
  • Thank you to mgri, George of all trades, and Knightshound. I implemented the above code and it works great! Thanks again, Caitlin – Caitlin Feb 14 '17 at 19:15

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


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

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.