How to enable word wrapping for legend labels?

261
2
Jump to solution
11-23-2021 11:47 AM
LesleyBross1
New Contributor III

Does anyone have a C# example of enabling word wrapping for legend labels? This setting is accessible on the Legend Arrangement Options in ArcMap but I can't find a way to set it in the API.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
NarelleChedzey
Esri Contributor

Hi Lesley, 

There isn't a property for directly setting label wrapping on the legend object.  But you can access the property via the CIM.    Here's an example of how you do it. 

  await QueuedTask.Run(() => 
  {
    var layout = LayoutView.Active.Layout;
    var legend = layout.FindElement("Legend") as Legend;
    if (legend == null)
      return;

    // get the CIM definition
    var def = legend.GetDefinition() as CIMLegend;

    // set label wrapping to 1 inch

    var labelWidthInches = 1;
    // CIM stores in points, so convert
    var labelWidthPoints = LinearUnit.Inches.ConvertTo(labelWidthInches, LinearUnit.Points);

    // assigning a non-zero value sets the wrapping
    def.LabelWidth = labelWidthPoints;

    // Zero value turns off description wrapping
    def.DescriptionWidth = 0;

    // update the CIM definition
    legend.SetDefinition(def);
  });

 

Thanks

Narelle

 

 

View solution in original post

2 Replies
NarelleChedzey
Esri Contributor

Hi Lesley, 

There isn't a property for directly setting label wrapping on the legend object.  But you can access the property via the CIM.    Here's an example of how you do it. 

  await QueuedTask.Run(() => 
  {
    var layout = LayoutView.Active.Layout;
    var legend = layout.FindElement("Legend") as Legend;
    if (legend == null)
      return;

    // get the CIM definition
    var def = legend.GetDefinition() as CIMLegend;

    // set label wrapping to 1 inch

    var labelWidthInches = 1;
    // CIM stores in points, so convert
    var labelWidthPoints = LinearUnit.Inches.ConvertTo(labelWidthInches, LinearUnit.Points);

    // assigning a non-zero value sets the wrapping
    def.LabelWidth = labelWidthPoints;

    // Zero value turns off description wrapping
    def.DescriptionWidth = 0;

    // update the CIM definition
    legend.SetDefinition(def);
  });

 

Thanks

Narelle

 

 

LesleyBross1
New Contributor III

This works fine. It is quite obscure and I would never have found it on my own. It would be nice if this could be added as a property. Thanks for your help!

0 Kudos