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.

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.tsimport { 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];
};
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
rich-text-labels.react.tsximport type { FeatureLike } from 'ol/Feature.js';
import Fill from 'ol/style/Fill.js';
import Stroke from 'ol/style/Stroke.js';
import Style from 'ol/style/Style.js';
import Text from 'ol/style/Text.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
GeoJSONSource,
Map,
VectorLayer,
View,
} from 'cc-openlayers/react';
const areaStyle = new Style({
fill: new Fill({ color: 'rgba(255, 255, 255, 0.6)' }),
stroke: new Stroke({ color: '#319FD3', width: 1 }),
});
const 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 [areaStyle, label];
};
/**
* 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.
*/
export function RichTextLabels() {
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={7}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<VectorLayer background="white" style={style}>
<GeoJSONSource url="assets/poland/voivodeships.geojson" />
</VectorLayer>
</Map>
);
}How the React version works
Voivodeship labels with the name in bold followed by the code in the normal weight, as one rich text; at zoom 7 the labels in view fit their voivodeships.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
<aol-layer-vector> with <aol-source-geojson> | <VectorLayer> with <GeoJSONSource> |
a style function as a class field bound to [style] | a style function constant in the module, passed as style; a new function in every render would restyle the layer each time |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
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
textto an alternating array pairs the province name ('bold 13px Calibri,sans-serif') with the TERYT code ('', inheriting basefont). - 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.
Related examples
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.