Styling and labels · Angular, React and OpenLayers

Labels in two fonts

Label Polish voivodeships in OpenLayers with multi-font rich text arrays pairing bold province names with regular-weight two-digit TERYT codes.

Southern Poland's voivodeships around Kraków in white with blue borders, each labelled with its name in bold and its two-digit code.
A screenshot of the example. Voivodeships: Państwowy Rejestr Granic © GUGiK.

What this example shows

Opening at zoom 7 over southern Poland around Kraków, each voivodeship polygon is styled in translucent white with a blue border and centred with a single composite label showing its Polish name in bold followed immediately by its official two-digit TERYT statistical code in normal weight.

When you need it

Cadastral parcels, administrative regions, utility poles and environmental stations routinely combine a primary identifier in bold with a secondary metric, unit or code in lighter type.

Formatting both parts as a single OpenLayers rich-text array keeps the entire compound label centred and decluttered as one indivisible unit rather than two separate Text styles with fragile manual pixel offsets.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

rich-text-labels.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerVectorComponent,
  MapComponent,
  SourceGeoJSONComponent,
  ViewComponent,
} from 'cc-openlayers';
import type { FeatureLike } from 'ol/Feature';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import Text from 'ol/style/Text';

/**
 * Voivodeship labels in two fonts: the name in bold and the code in the normal weight, as one rich text. At zoom 7
 * every label in view fits its voivodeship; at zoom 6, most labels would be wider than their voivodeships and left out.
 */
@Component({
  selector: 'app-rich-text-labels',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerVectorComponent,
    MapComponent,
    SourceGeoJSONComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="7"><aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" /></aol-view>
      <aol-layer-vector background="white" [style]="style">
        <aol-source-geojson url="assets/poland/voivodeships.geojson" />
      </aol-layer-vector>
    </aol-map>
  `,
})
export class RichTextLabels {
  private readonly areaStyle = new Style({
    fill: new Fill({ color: 'rgba(255, 255, 255, 0.6)' }),
    stroke: new Stroke({ color: '#319FD3', width: 1 }),
  });

  protected readonly style = (feature: FeatureLike): Style[] => {
    const label = new Style({
      text: new Text({
        text: [String(feature.get('name')), 'bold 13px Calibri,sans-serif', ` ${feature.get('code')}`, ''],
        font: '13px Calibri,sans-serif',
        fill: new Fill({ color: '#000' }),
        stroke: new Stroke({ color: '#fff', width: 4 }),
      }),
    });
    return [this.areaStyle, label];
  };
}

Architecture and OpenLayers mechanics

Instead of a single string, the Text style’s text property receives an alternating [string, font, string, font, ...] array: first the voivodeship name paired with 'bold 13px Calibri,sans-serif', followed by a space and TERYT code paired with '' (which inherits the Text style’s base font).

OpenLayers places polygon labels at an interior horizontal chord midpoint so curved or concave shapes keep their text comfortably inside the border.

How it works

  • Setting text to an alternating array pairs the province name ('bold 13px Calibri,sans-serif') with the TERYT code ('', inheriting base font).
  • The style function returns two styles per polygon: a translucent white fill with blue stroke, plus its composite rich-text label.
  • Polygon labels anchor at the midpoint of the widest horizontal span passing through the polygon’s vertical centre.
  • At initial zoom 7 every visible voivodeship is wide enough to contain its full two-part label cleanly.

Pitfalls

  • Line breaks inside OpenLayers rich-text arrays must be passed as their own line-break pair, and rich-text arrays are supported only for point/interior placement—not for curved line-placement paths or toContext immediate rendering.
  • With default overflow: false, OpenLayers hides any polygon label wider than its polygon’s interior chord—zooming out one notch drops most labels unless overflow: true is enabled.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.