Is there a way to set multiple font colors for ol.style.Text class for openlayers 4+?
I am trying to do something like
const label = new ol.style.Style({
text: new ol.style.Text({
text: '<color1>X</color1> other text',
textAlign: 'center',
font: '11px roboto,sans-serif',
fill: new ol.style.Fill({
color: 'white'
}),
stroke: new ol.style.Stroke({
color: 'black',
lineCap: 'butt',
width: 4
}),
offsetX: 0,
offsetY: 25.5,
})
since "other text" length or width is unknown and textAlign must be set to center, i can't add two ol.style.Text classes and position them side by side.
Thanks in advance
There is no solution for this on the library level, but you can quite easily achieve it with two text styles. The trick is to measure the width of both texts using e.g.
CanvasRenderingContext2D.measureText
, and to adjust theoffsetX
property of both text styles accordingly: