8

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.

4 Answers 4

8

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

6

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', '')
2
  • +1 Good point, I think that Caitlin knows all the possible situations by now! =)
    – mgri
    Feb 14, 2017 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, 2017 at 19:15
2

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
0

Yet another addition: It is possible to store a CASE statement like this:

CASE 
  WHEN @map_scale < 100000 THEN 18  
  WHEN @map_scale < 1000000 THEN 16
  WHEN @map_scale < 10000000 THEN 14
  ELSE 12           
END

in a project variable, e.g. my_font_size, and then use the expression EVAL(@my_font_size) as a data defined override for a font size

enter code here

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.