Tuesday, 13 August 2019

labeling - Dashed label underline in QGIS



Is it possible to create a dashed underline for labeling in QGIS? I have one capital city which for used underline for the name of this city. and now I need dashed underline to a name of the province, how to create dashed underline for the name of the cities in qgis?



Answer



Here is a workaround for 2.x.


Having a point shape-file with an attribute city you can label the cities like:


 "city"  || '\n' ||  rpad( '-', length("city") + 5,  '-')

BEST FIT - additional lpad (extend to the left) without addition of a distinct value:


"city"  || '\n' ||  rpad('-',length("city"),'-') ||  lpad('-',length ("city"),'-')

First argument is the city name, then follows a line break, then we us a hypen and extend (rpad and/or lpad) it with the length of the city names (length("city") and/or add a distinct value to stretch the concatenation of hyphens.



enter image description here


EDIT - Using other Unicode characters


You could also use Unicode symbols directly (see https://gis.stackexchange.com/a/90126). Using a fullwidth hyphen-minus from http://jkorpela.fi/dashes.html (lowermost entry in the first column of the table), the dashed line looks even better. Just copy/paste it in the Expression Dialog. I've shortened it a little bit (-2).


"city"  || '\n' ||  rpad('-',length("city")-2, '-')

enter image description here


Label lookalike with katakana-hiragana double hyphen:


enter image description here


Additionally you have to decrease the line height in the Labels > Formatting. So the dash line is closer to the city name. Also use Center as Alignment to center both lines.


Usage in the Layer Properties:



enter image description here


Result


enter image description here


I think you have to play around a little bit with the distinct value for stretching the concatenation of hyphens.


EDIT


If there is an attributes that distinguish if the city is a capital or not then you can use Rule-based labeling.


In my case I have an attribute table as follows:


enter image description here


Rule-based labeling in this case:


enter image description here



Result (cities which are not capitals have a normal underline (defined within in the label settings):


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...