在使用的react版本如下:
const label = new BMapGL.Label('浦东99套', opts);
// 自定义文本标注样式
label.setStyle({
cursor: 'pointer',
height: '50px',
width: '50px',
lineHeight: '50px',
fontFamily: '微软雅黑',
fontSize: '12px',
textAlign: 'center',
color: 'rgb(255,255,255)',
padding: '0px',
border: '1.5px solid white',
borderRadius: '50%',
backgroundColor: 'green'
});
map.addOverlay(label)
目前效果如下:
const label = new BMapGL.Label('<p>浦东</p><p>99套</p>', opts);
请问如何才能让覆盖物内的文字进行换行居中在覆盖物范围内(即文字不超出覆盖物边界)?
观察了一下,生成的label
元素中,white-space
默认为nowrap
,所以label
中的文本不会自动换。将white-space
设置为initial
即可。同时为了垂直水平居中,这里就是用flex
了。
label.setStyle({
display: 'flex',
alignItems: 'center',
cursor: 'pointer',
height: '50px',
width: '50px',
fontFamily: '微软雅黑',
fontSize: '12px',
textAlign: 'center',
color: 'rgb(255,255,255)',
padding: '0px',
border: '1.5px solid white',
borderRadius: '50%',
backgroundColor: 'green',
whiteSpace: 'initial '
})