Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm interested in wrapping long legend text (in ArcGIS 9.3.1) so that the text does not exceed the layout extent. I would like to call the "GetFormattedString" Function within the "UIButtonControl1_Click" procedure. Any pointers on how to get the legend text to wrap at 25 characters would be great.

Thank you!

Here is the code:

Public Sub UIButtonControl1_Click()

'Refresh legend

    Dim pMxDoc2 As IMxDocument
    Dim pPageLayout2 As IPageLayout
    Dim pGC2 As IGraphicsContainer
    Dim pElem2 As IElement
    Dim pMSF2 As IMapSurroundFrame
    Dim pMS2 As IMapSurround

    Set pMxDoc2 = ThisDocument
    Set pPageLayout2 = pMxDoc2.PageLayout
    Set pGC2 = pPageLayout2
    pGC2.Reset

    Set pElem2 = pGC2.Next
        Do Until pElem2 Is Nothing
        If TypeOf pElem2 Is IMapSurroundFrame Then
            Set pMSF2 = pElem2
            Set pMS2 = pMSF2.MapSurround

                Dim strFormattedString As String
                strFormattedString = GetFormattedString("TextContentblahblah", "Arial", 5, 12)

        pMS2.Refresh
        pMxDoc2.ActiveView.Refresh

        End If
        Set pElem2 = pGC2.Next

        Loop

End Sub

Private Function GetFormattedString(strTextContent As String, strFontName As _
String, sngFontSizeInPoints As Single, sngMaxWidthInPoints) As String

    Dim varWordArray As Variant
    Dim pMxApp As IMxApplication
    Dim pAppDisplay As IAppDisplay
    Dim pTransformation As ITransformation
    Dim pTextFont As IFontDisp
    Dim pTextSymbol As ITextSymbol
    Dim dblXSize As Double
    Dim dblYSize As Double
    Dim strGoodWidth As String
    Dim strFinalString As String
    Dim strTestString As String
    Dim i As Integer

    '------ Split the string into an array of words
    varWordArray = Split(strTextContent, " ", , vbTextCompare)

    Set pMxApp = Application 'QI
    '------ Get a pointer to the application display
    Set pAppDisplay = pMxApp.Display 'QI
    '------ Set up the Transformation
    Set pTransformation = pAppDisplay.DisplayTransformation
    '------ Set up the Font
    Set pTextFont = New StdFont
    pTextFont.Name = strFontName
    pTextFont.size = sngFontSizeInPoints
    '------ Set up the TextSymbol
    Set pTextSymbol = New TextSymbol
    pTextSymbol.font = pTextFont

    '------ Add each word into the test string and test for width
    pAppDisplay.StartDrawing pAppDisplay.hdc, 0
    For i = LBound(varWordArray) To UBound(varWordArray)
        If strTestString = "" Then
            strTestString = varWordArray(i)
        Else
            strTestString = strTestString & " " & varWordArray(i)
        End If
        '------ Get the TextSize
        pTextSymbol.GetTextSize pAppDisplay.hdc, pTransformation, strTestString, dblXSize, dblYSize
        '------ If the word added is < max width keep adding to the line, else make new  line by inserting vbcrlf
        If dblXSize < sngMaxWidthInPoints Then
            strGoodWidth = strTestString
        Else
            strTestString = varWordArray(i)
            strFinalString = strFinalString & vbCrLf & strGoodWidth
        End If

    Next i
    strFinalString = strFinalString & vbCrLf & strGoodWidth
    pAppDisplay.FinishDrawing
    'remove first vbcrlf and pass back
    GetFormattedString = Right$(strFinalString, Len(strFinalString) - 2)

End Function
share|improve this question

1 Answer 1

OK this does not directly answer your question but have you thought about just using the ArcGIS Gallery Script (formerly ArcScripts) Legend Limiter

http://arcscripts.esri.com/details.asp?dbid=16097

I understand that you may want the control of programming it yourself but just a thought.

Hope this helps,

CDB

share|improve this answer
    
I think his question is referring to the length of the text in the legend being to long and making the size of the legend wider than the layout, as opposed to limiting the legend to only include those layers and symbol types that are inside the data frame extents. –  Get Spatial May 19 '12 at 20:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.