Forcing label new line width

1232
3
03-04-2021 11:37 AM
Labels (1)
Matt_Trebesch
New Contributor III

Using ArcGIS Pro 2.7.1 with the Maplex labeling engine.

Trying to figure out how to force the maximum width of a label.

I have created a label expression in Arcade:

"Species: " +  DomainName($feature, 'spp')
+TextFormatting.NewLine
+ "Height: " + $feature.height
+TextFormatting.NewLine
+"DBH: " +$feature.dbh
+TextFormatting.NewLine
+"Notes: " + $feature.longtermnotes

 

And the output I get is:

Species: spruce, engelmann
Height: 55
DBH: 28
Notes: Already half way through life span in this planting strip.

 

How can I force a paragraph width of say 25 characters?  I'm trying to avoid a long label width from "longtermnotes" field (last label line)

I have found when I go to the label properties:

Position: Stack options, that no mater what I put in for the "Maximum Characters per line" value that it won't limit the width of the label.

Is there a programmatic way to do that with Arcade or a text formatting tag?

Matt_Trebesch_0-1614886616611.png

 

Tags (3)
3 Replies
MarkVolz
Occasional Contributor III

I bet there is a better way of working around the issue, but you could try adding a stacking separator such as "*" and making it not visible.

0 Kudos
Matt_Trebesch
New Contributor III

yeah, I thought about that, but I'm hoping for a programmatic solution that doesn't require altering data, but your suggestion is appreciated!

0 Kudos
JesseWickizer
Esri Contributor

You could add a function to the Arcade expression to modify the longtermnotes field. That function could add line breaks at the desired intervals and return the modified notes that you can output in your original label expression. Here's an updated label expression that uses an Arcade function to add a line break once there are at least 24 characters in a line.

function FormatNotes(notes){
  var formattedNotes = "";
  var lineCharCount = 0;
  var wordArray = Split(notes, ' ');
  for (var i in wordArray) {
    if (lineCharCount >= 24) {
      lineCharCount = Count(wordArray[i]);
      formattedNotes += TextFormatting.NewLine + wordArray[i];
    }
    else {
      lineCharCount += (1 + Count(wordArray[i]));
      formattedNotes += " " + wordArray[i];
    }
  }
  return formattedNotes;
}

"Species: " + DomainName($feature, 'spp') + TextFormatting.NewLine +
"Height: " + $feature.height + TextFormatting.NewLine +
"DBH: " + $feature.dbh + TextFormatting.NewLine +
"Notes: " + FormatNotes($feature.longtermnotes)

If you need to limit each line to a max of 25 characters you'd have to add some additional checks on the next word but this should give you a good starting point.

Here's an example label with this expression applied:

JesseWickizer_0-1650558836932.png