Wednesday 6 March 2019

labeling - ArcObjects add halo to label


I would like to add a white halo to labels using ArcObjects. I think I need to reference the IFormattedTextSymbol interface, but I'm not sure how to implement this into my current code (see below).



Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Set pMap = pDoc.FocusMap
' Get the selected layer
Dim pLayer As IGeoFeatureLayer
Set pLayer = pMap.Layer(0)

Dim pFc As IFeatureClass
Dim strOIDName As String
Set pFc = pLayer.FeatureClass

strOIDName = pFc.OIDFieldName

' Get the selected features from the layer
Dim pFSel As IFeatureSelection
Set pFSel = pLayer
Dim pSelSet As ISelectionSet
Set pSelSet = pFSel.SelectionSet
Dim pFCur As IFeatureCursor
pSelSet.Search Nothing, False, pFCur


' Loop through the selected features and create a label
' expression for the selected features

Dim pFeat As IFeature
Dim strSql As String
Set pFeat = pFCur.NextFeature

Do While Not pFeat Is Nothing
If strSql = "" Then
strSql = strOIDName & " = " & pFeat.OID

Else
strSql = strSql & " or " & strOIDName & " = " & pFeat.OID
End If
Set pFeat = pFCur.NextFeature
Loop
Debug.Print strSql

' Get AnnotateLayerPropertiesCollection from layer
Dim pAnnoLayerPropsColl As IAnnotateLayerPropertiesCollection
Set pAnnoLayerPropsColl = pLayer.AnnotationProperties

Dim pAnnoLayerProps As IAnnotateLayerProperties
pAnnoLayerPropsColl.QueryItem 0, pAnnoLayerProps, Nothing, Nothing
pAnnoLayerProps.Class = "LabelSel"
pAnnoLayerProps.WhereClause = strSql
Dim aAnnoVBScriptEngine As IAnnotationExpressionEngine
Set aAnnoVBScriptEngine = New AnnotationVBScriptEngine

Dim pTextsymbol As ITextSymbol
Set pTextsymbol = New TextSymbol


Dim pFont As IFontDisp
Set pFont = pTextsymbol.font

Dim pFormattedtxtsym As IFormattedTextSymbol
pFormattedtxtsym.ShadowColor = True

pFont.Bold = True
pFont.size = 10
pFont.Weight = 2
pTextsymbol.font = pFont


Dim pLELayerProps As ILabelEngineLayerProperties
Set pLELayerProps = pAnnoLayerProps
Set pLELayerProps.ExpressionParser = aAnnoVBScriptEngine

pLELayerProps.Expression = "[Longlabel] & vbnewline & [Latlabel] & vbnewline & [Elevlabel] & _ vbnewline &[Driftlabel]"

Set pLELayerProps.Symbol = pTextsymbol

' Display the lables

pLayer.DisplayAnnotation = True
' Refresh the Data Frame
pDoc.ActiveView.Refresh

Answer



Halos are specified through the IMask interface which the TextSymbol class implements:


Dim pMask As IMask
Dim pMaskFillSymbol As IFillSymbol
Dim pMaskFillColor As IRgbColor

Set pMask = pTextSymbol ' your textsymbol

pMask.MaskStyle = esriMSHalo
pMask.MaskSize = 1 ' halo size

' red halo around text, modify to your desired color
Set pMaskFillSymbol = New SimpleFillSymbol
Set pMaskFillColor = New RgbColor
pMaskFillColor.Red = 255
pMaskFillColor.Green = 0
pMaskFillColor.Blue = 0
pMaskFillSymbol.Color = pMaskFillColor


Set pMask.MaskSymbol = pMaskFillSymbol

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...