6

This question has been asked in the past but those questions were either not answered or suggested solutions that required multiple labels separated by newlines and placement rules to simulate a contiguous label.

Does the label engine in QGIS currently allow styling substrings based on rules? I'm writing a quick tutorial for a GIS class on labelling, and I'm seeing if a label like this:

"name_en" ||  '\n'  ||  'is a ' || lower("category") || ' site which is ' || case when "danger" = 1 then 'under threat.' else 'not under threat.' end 

Can be modified so that the contents of the field "name_en" is displayed in bold, whereas the rest of the string is displayed using regular font. For reference, the data in question is UNESCO's list of World Heritage Sites, available here.

I can get there by splitting them up in two and devising a rule for "name_en" separately, but that feels like a wonky solution. I've tried several expressions in the 'data defined override' button, but the result always comes back as either all bold or all regular. It expects a boolean argument, so that kind of assumes the output (i.e. any given label) will always be either all bold or all regular.

I assume the answer will involve something like using the length of the name_en string to apply the bold style to the first n characters, or:

left(len(name_en))

But I'm not finding a way to use that kind of logic to style a substring of a label.

  • 3
    Short answer: No. Longer answer: Not yet. Even longer answer: Using different colors is on the todo list for QGIS 3.14, but bold is still looking for a sponsor. – Matthias Kuhn Apr 5 at 16:03
  • Thank you for the clarification @MatthiasKuhn! – Encomium Apr 5 at 16:23
15

If you are not bound to a specific font (those characters might be missing for some), you can abuse special Unicode characters for this. This is the same trick people use to have bold or cursive text on social media.

https://boldtext.io/ has a pretty thorough explanation and the characters ready to copy and paste.

The other pre-condition is that you must be willing to do some awful hacking.

You can use the special characters with the replace function in the label expression. Your expression will become really awkward, long and hard to maintain if you start having too much fun.

Here is an example using Natural Earth data, you can enter world in the coordinate indicator at the bottom of the QGIS screen and hit Enter to load the data set I used.

with_variable(
  'normal_characters',
  array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'),
  with_variable(
    'bold_characters',
    array('๐—ฎ','๐—ฏ','๐—ฐ','๐—ฑ','๐—ฒ','๐—ณ','๐—ด','๐—ต','๐—ถ','๐—ท','๐—ธ','๐—น','๐—บ','๐—ป','๐—ผ','๐—ฝ','๐—พ','๐—ฟ','๐˜€','๐˜','๐˜‚','๐˜ƒ','๐˜„','๐˜…','๐˜†','๐˜‡','๐—”','๐—•','๐—–','๐——','๐—˜','๐—™','๐—š','๐—›','๐—œ','๐—','๐—ž','๐—Ÿ','๐— ','๐—ก','๐—ข','๐—ฃ','๐—ค','๐—ฅ','๐—ฆ','๐—ง','๐—จ','๐—ฉ','๐—ช','๐—ซ','๐—ฌ','๐—ญ','๐Ÿฌ','๐Ÿญ','๐Ÿฎ','๐Ÿฏ','๐Ÿฐ','๐Ÿฑ','๐Ÿฒ','๐Ÿณ','๐Ÿด','๐Ÿต'),
    replace("NAME", @normal_characters, @bold_characters)
    || '\n'  ||  'has the ISO A3 code ' ||
    with_variable(
      'fraktur',
      array('๐–†','๐–‡','๐–ˆ','๐–‰','๐–Š','๐–‹','๐–Œ','๐–','๐–Ž','๐–','๐–','๐–‘','๐–’','๐–“','๐–”','๐–•','๐––','๐–—','๐–˜','๐–™','๐–š','๐–›','๐–œ','๐–','๐–ž','๐–Ÿ','๐•ฌ','๐•ญ','๐•ฎ','๐•ฏ','๐•ฐ','๐•ฑ','๐•ฒ','๐•ณ','๐•ด','๐•ต','๐•ถ','๐•ท','๐•ธ','๐•น','๐•บ','๐•ป','๐•ผ','๐•ฝ','๐•พ','๐•ฟ','๐–€','๐–','๐–‚','๐–ƒ','๐–„','๐–…'),  -- no numbers
      if(
        "ISO_A3" = 'DEU',
        replace("ISO_A3", array_slice(@normal_characters, 0, 51), @fraktur),  -- only the first 52 normal characters
        "ISO_A3"
      )
    )
  )
)

enter image description here

| improve this answer | |
  • 3
    Great knowledge @bugmenot123 -- thanks for sharing! – Encomium Apr 5 at 18:18
  • Amazing solution! – Val P Apr 5 at 20:36
  • What a nice hack! Thank you. – muhalif Apr 9 at 7:12

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.