I am confused on how to use the maximum line length for word wrapping a label in QGIS 3. The dialog and documentation show an option to "Wrap lines to" and then allow selecting "Maximum line length" but where do you specify the length?

I want to actually constrain the label to certain width (maybe even a rectangle) and have the lines split into multiple lines based upon the width.

Here is the documentation:

enter image description here


You need to set a number above, in the field named Wrap lines to where now you have No automatic wrapping. It's not so intuitive, indeed.

In the following screenshot, you can see how different settings of maximum and minimum line length for word wrapping affects the label. It's alwasy the same label, the only difference is that I changed the wrap lines to value for maximum line length and minimum line lenght.

If you choose 4 characters max., after "this" we have the first wrap as the word is exactely 4 characters long. On the next line, we have just "is" (2 char.), because including the next word including the space character "is my" results already in 5 characters, thus more as we defined as maximum.

However, below, with a setting of 4 characters min., the first line is the same, it it has exactely 4 characters, thus the minimal number required is reached an a wrap is introduced. The second line is different: a word wrapping after "is" (as with the max. setting) would result in a 2 character line, thus too short for the minimal setting of 4 we defined: thus in this case, we have 5 characters on the second line: "this is". And so on.

As you can see, QGIS does not wrap inside of words - thus even with a setting of max. 4 characters, you get longer lines: "consists" (8 char.) or "definitely" (10 characters) are not wrapped.

QGIS setting maximum and minimum line length for word wrapping

If you want to have a text box (rectangle) around your label, this can be easily done. Go to the background tab and activate a rectangular background, setting size, color and everything you like. Than the box will always adapt to your text size and length of the rows. By the way, in the following screenshot, I used the Wrap on character function to get the text on two lines.

enter image description here

However, the expression editor gives you a lot of possibilities to adapt word wrap so that you can define for example that the text should wrap in such a way as to get at least three rows or not more than two rows. See the next screenshot, where I used the expression to_int (length ("text" ) / 2) to have a data driven value for minimum word wrap to force two lines, indifferent of how long the text is: to_int (length ("text" ) / 2)

enter image description here

  • Thanks so much for the explanation! This works very differently than I expected. It does not break apart words as you mentioned but seems to break at either the end or the beginning based on the maximum or minimum line length. Very interesting! I need to play around with this a bit but this helps a lot! But I am thinking that I am more wanting to define a polygon / rectangle and have the label constrained inside of it. Or maybe use the min and max and then build a rectangle around it might work. Or maybe just use the label formatting to set a background and border! – bdelliott Dec 6 '20 at 16:33
  • You're welcome. Added to my answer how to build a rectangle to the label – Babel Dec 6 '20 at 16:50

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.