Building label expressions
You can use label expressions to adjust the formatting of your labels. In addition to inserting characters and scripting functions, you can use ArcGIS formatting tags in label expressions. These are special characters for changing the appearance of all or part of your labels. For example, you might use the bold formatting tag to make the first line bold in a stacked, multiline label.
A label expression is limited to a single line of code unless you check the Advanced box on the Label Expression dialog box. Checking the Advanced box allows you to enter a function containing programming logic and spanning multiple lines of code.
Steps:
- Click the Label Manager button on the Labeling toolbar.
- Click a label class in the Label Classes list.
- Click the Expression button.
- Choose a language on the Parser menu.
- Type a VBScript or JScript expression. Optionally, enter ArcGIS text formatting tags in the Expression box to apply formatting to a portion of your label text.
If your expression will span multiple lines of code check the Advanced checkbox and then enter your label expression.
- Click Verify to make sure there are no syntax errors.
- Click OK on each of the dialog boxes.
Expression examples
The following are examples of label expressions:
- Concatenate a string to the value in a field. For example, this expression creates a label where the value of the PARCELNO field is preceded by the text "Parcel no:":
VBScript
"Parcel no: " & [PARCELNO]
"Parcel no: " + [PARCELNO]
- Round a decimal number to a set number of decimals. For example, this expression displays an Area field rounded to one decimal place:
VBScript
Round ([AREA], 1)
function FindLabel ( [AREA] ) { var ss; var num= parseFloat([AREA]); ss = num.toFixed(1); return (ss); }
- Convert your text labels to all uppercase or lowercase. For example, this expression makes a Name field all lowercase:
VBScript
LCase ([NAME])
[NAME].toLowerCase()
- Convert your text labels to proper case. For example, this expression takes a Name field that is in all capitals and makes it proper case:
VBScript
Function FindLabel ( [NAME] ) FindLabel = UCase(Left([NAME],1)) & LCase(Right([NAME], Len([NAME]) -1)) End Function
function FindLabel ( [NAME] ) { var str = [NAME]; var iLen = String(str).length; var upper = (str.substring(0,1)).toUpperCase(); var lower = (str.substring(1, iLen)).toLowerCase() return upper + lower; }
- Create stacked text. For example, this expression creates a label with the Name field and the two address fields all on separate lines:
VBScript
"Name: " & [NAME] & vbCrLf& [ADDRESS_1] & vbCrLf& [ADDRESS_2]
"Name: " + [NAME] + "\r" + [ADDRESS_1] + "\r" + [ADDRESS_2]
- Create stacked text based on text from one field. For example, this expression uses the comma to specify where the stack happens:
VBScript
Function FindLabel ( [LABELFIELD] ) FindLabel = replace([LABELFIELD], ", ", vbnewline) End Function
function FindLabel ( [LABELFIELD] ) { var r, re; var str = [LABELFIELD]; re = /,/g; r = str.replace(re, "\r"); return r; }
- Format your labels. For example, this expression displays the label as currency:
VBScript
"Occupancy Revenue: " & FormatCurrency ([MAXIMUM_OC] * [RATE])
function FindLabel ( [MAXIMUM_OC], [RATE] ) { var ss; var num1 = parseFloat([MAXIMUM_OC]); var num2 = parseFloat([RATE]); var num3 = num1 * num2 ss = num3.toFixed(2); return ("$" + ss); }
- Specify a conditional if-else statement. These functions label cities with their name in a large, red font if their population is equal to or exceeds 250,000 and in the default label font if the population is less than 250,000:
VBScript
Function FindLabel ([NAME], [POPULATION]) if ([POPULATION] >= 250000) then FindLabel = "<CLR red='255'><FNT size = '14'>" + [NAME] + "</FNT></CLR>" else FindLabel = [NAME] end if End Function
function FindLabel ( [NAME], [POPULATION] ) { if ([POPULATION] >= 250000){ return ("<CLR red='255'><FNT size = '14'>" + [NAME] + "</FNT></CLR>"); } else return ([NAME]); }
Note:To label a subset of features based on field value, create the SQL query in the label class instead of through the label expression.
Learn more about Microsoft VBScript Language Reference
Learn more about Microsoft JScript Language Reference
(This information is housed on Web pages created, owned, and maintained by Microsoft Corporation. ESRI cannot guarantee the availability of these pages and is not responsible for the content found on them.)
ArcGIS text formatting tags
Labels are drawn using the symbol specified in the Label Manager or on the Labels tab of the Layer Properties dialog box. You can modify or override the appearance of this symbol for particular portions of the expression by inserting ArcGIS text formatting tags into the expression as text strings. This lets you create mixed-format labels where, for example, one field in a label is underlined.
The tags that you can use are listed in the table below. Acceptable values for Color (RGB) are red, green, blue = 0–255, and acceptable values for Color (CMYK) are cyan, magenta, yellow, black = 0–100; missing color attributes are assumed to be 0.
Font |
"<FNT name='Arial' size='18'>" & [LABELFIELD] & "</FNT>" "<FNT name='Arial' scale='200'>" & [LABELFIELD] & "</FNT>" |
Color |
"<CLR red='255' green='255' blue='255'>" & [LABELFIELD] & "</CLR>" "<CLR cyan='100' magenta ='100' yellow='100' black='100'>" & [LABELFIELD] & "</CLR>" |
Bold |
"<BOL>" & [LABELFIELD] & "</BOL>" |
Italic |
"<ITA>" & [LABELFIELD] & "</ITA>" |
Underline |
"<UND>" & [LABELFIELD] & "</UND>" |
All capitals |
"<ACP>" & [LABELFIELD] & "</ACP>" |
Small capitals |
"<SCP>" & [LABELFIELD] & "</SCP>" |
Superscript |
"<SUP>" & [LABELFIELD] & "</SUP>" |
Subscript |
"<SUB>" & [LABELFIELD] & "</SUB>" |
Character spacing (0%=regular) |
"<CHR spacing='25'>" & [LABELFIELD] & "</CHR>" |
Character width (100%=regular) |
"<CHR width='150'>" & [LABELFIELD] & "</CHR>" |
Word spacing (100%=regular) |
"<WRD spacing='150'>" & [LABELFIELD] & "</WRD>" |
Line leading (pts) |
"<LIN leading='12'>" & [LABELFIELD] & "</LIN>" |
Un-Bold |
"<_BOL>" & [LABELFIELD] & "</_BOL>" |
Un-Italic |
"<_ITA>" & [LABELFIELD] & "</_ITA>" |
Un-Underline |
"<_UND>" & [LABELFIELD] & "</_UND>" |
Un-Superscript |
"<_SUP>" & [LABELFIELD] & "</_SUP>" |
Un-Subscript |
"<_SUB>" & [LABELFIELD] & "</_SUB>" |
Tag syntax
The following syntax rules apply to tags in label expressions:
- Formatting tags must be surrounded by double quotes and concatenated to other parts of the expression using the & operator:
"<BOL>" & [LABELFIELD] & "</BOL>"
"Current <BOL>status</BOL> of parcel: " & [LABELFIELD]
- The ArcMap text formatting tags follow XML syntax rules. Each start tag must be accompanied by an end tag. Tags can be nested, but you must close the inner tag before closing an outer tag:
"<BOL><UND>" & [LABELFIELD] & "</UND></BOL>"
- The case of tag pairs must match exactly. So <BOL>...</BOL> is valid, as is <bol>..</bol>, but <Bol>...</bol> is invalid.
- In label expressions, tag attributes must be surrounded either by single quotes or by two sets of double quotes. The following expressions are equivalent:
"<FNT name=""Arial"" size=""18"">" & [LABELFIELD] & "</FNT>"
"<FNT name='Arial' size='18'>" & [LABELFIELD] & "</FNT>"
- The special characters & and < are not valid in your text if formatting tags are used. Use the equivalent character codes & and < instead. For example, this expression displays the values of the label field inside < > characters:
"<ITA><" & [LABELFIELD] & "></ITA>"
Function FindLabel ([LABELFIELD]) NewString = Replace([LABELFIELD],"&","&") FindLabel = "<ITA>" & NewString & "</ITA>" End Function
- Formatting tags can be embedded in the values of the field you use to label a layer's features whether or not you use a label expression. In this way, you can change the format of any portion of a particular value in a label field. To embed formatting tags, the label field must be of string type. Tags and tag attributes used in field values do not need to be surrounded by quotes, so the following are valid values for a label field:
<ITA>Rochester</ITA> <FNT size='18'>C</FNT>Colorado
Tips for building label expressions
The following tips will help you build your label expressions:
- To check the validity of expressions containing text formatting tags, click the Verify button or Apply your changes and view the labels on the map. In the case of invalid formatting tags or syntax, tags appear as plain text in the Verify window and map labels.
- Text formatting tags can be used almost anywhere you place text on or around the map.