ThinkGeo.com    |     Documentation    |     Premium Support

Displaying word-wrapped text

In desktop edition 2.0 I was able to overide a shape's CustomDraw method in order to display word-wrapped text. I used the dimensions of the shape (a rectangle) to bound the word-wrapped text. How can I acheive this in desktop edition 3.0?



Greg,



  Really in the same way.  You can create your own Style either inherit from Text style or inherit from Style and override the DrawCore.  Inside of there you have access to the Canvas (GeoCanvas) and the features to draw.  You can get the bounding box from the feature along with all the column values.  You can loop through each feature and call the Canvas.DrawText or some API like that.  It is very similar to the GDI+ call to draw text.


Below is a NON working example that shows you some points above




    // This code will NOT work as is but as an example with some helpfull methods highlighted
    public class WordWrapStyle : Style
    {
        protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInThisLayer, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInAllLayers)
        {
            foreach (Feature feature in features)
            {
                // This gets the bounding box for the feature.  
                // It is not used but wanted to show you how to get it
                RectangleShape boundingBox = feature.GetBoundingBox();
                
                // This gets the size in pixels that the text will take up
                // It is not used but wanted to show you how to get it
                DrawingRectangleF drawingArea = canvas.MeasureText("Hello",new GeoFont("Arial", 15))
                
                // This draws some text.  Note I am passing in 0 for the location
                // This will not work
                canvas.DrawTextWithWorldCoordinate("Hello", new GeoFont("Arial", 15), new GeoSolidBrush(GeoColor.StandardColors.Black), 0, 0, DrawingLevel.LabelLevel);
            }
        }
    }
 




David



Thanks for the tip, David. I’m guessing I add this a custom style in my zoom level?

Gregory, 
  
 Yes, you are right. 
  
 If the WordWrapTextStyle inherits from TextStyle instead of Style, then you just need to set it in DefaultTextStyle instead of adding it to CustomStyles. 
  
 Thanks. 
  
 Yale 


Guys, 
  
   We have to be careful here becasue if you inherit from TextStyle and override the DrawCore then you override a bunch of stuff that the TextStyle is doing.  In doing so I would be wary of assuming that certain things are going to happen.   
  
 Another angle to approach this problem would be to inherit from TextStyle, override the DrawCore and just loop though the features and where you see a feature you want to wrap actually treat it as a new feature and give it a new geometry.  After you split them all send all of them to the DrawCoreBase and let all of the drawing logic do its thing.  This way you can leverage all of the TextStyle power and you are just meddling a little with what features get fed through.  If you are not sure about this let me know and I can cook up a little sample. 
  
 David

Guys,



 I let you know that today we posted a working version of a TextStyle that implements word-wrapping. You can check it out at:

code.thinkgeo.com/projects/show/wordwrapped

 


Thank you.



I’m impressed! Thanks so much for putting this project together. I’ll let you know if I have any issues with it…

Gregory, 


Sorry for the delay of responsing, we finally put the public release out :). Nny problems just feel free to let me know.
 
Thanks.
 
Yale

I have a custom style (for points) that inherits from Style, and now I want to add this wrapping label functionality, how can I take advantage of the TextStyle stuff (such as BestPlacement property) without inheriting from it? If I set a customstyle for a FeatureLayer I cannot have a DefaultTextStyle applied aswell, right?

Carlos, 
  
 Why you didn’t inherit from TextStyle, I think if you did you can take advantage of BestPlacement property. 
  
 By the way, could you provide your custom style and we can update it to make it. 
  
 Thanks, 
 James

Hi James, 



Thanks a lot for your support. I realized that I can use multiple CustomStyles at the same time, so I added the WordWrappedTextStyle that inherits from TextStyle to the CustomStyles collection, and seems to work, even thought the BestPlacement doesn't seem to detect the PointShape (Icon) to avoid it. 





Carlos.



Carlos, 
  
 Thanks for your feedback and letting us know your status. 
  
 Just curious did you set the OverlappingRule to avoid the overlapping?  
  
 Any more questions please feel free to let me know. 
  
 Thanks. 
  
 Yang 


Yes I did set it to NoOverlapping, but I guess it only refers to don’t overlay with other strings (that is actually what it’s doing) 
  
  I workarrounded the issue by now by using YOffsetInPixel, but I’m wondering if it is any better solution.

Carlos, 
  
 Sorry to tell you that as so far we don’t have the ability to implement it easily such as property or any other ways, I think that use the YOffsetInPixel is a good solution now. Please keep use the workaround on your application now we will consider to add the new ability to the core code in the next public release. 
  
 If you have any questions please let me know, 
  
 Thanks, 
  
 Scott,

Ok Scott, the workarround is sufficient for me right now. 
  
  Thanks, 
  
 Carlos.

Carlos, 
  
 Please let us know if you still have any other questions, 
  
 Thanks, 
  
 Scott,