CanvasContext.setTextBaseline

解释:用于设置文字的水平对齐。

# 方法参数

StringtextBaseline

textBaseline参数说明 :有效值 'top'、'bottom'、'middle'、'normal' 。

# 示例

扫码体验
重新加载
请使用百度APP扫码

# 图片示例

# 代码示例

    <canvas canvas-id="myCanvas" class="myCanvas" />
    
    Page({
        onLoad() {
            const canvasContext = swan.createCanvasContext('myCanvas');
    
            canvasContext.setStrokeStyle('red');
            canvasContext.moveTo(5, 75);
            canvasContext.lineTo(295, 75);
            canvasContext.stroke();
    
            canvasContext.setFontSize(20);
    
            canvasContext.setTextBaseline('top');
            canvasContext.fillText('top', 5, 75);
    
            canvasContext.setTextBaseline('middle');
            canvasContext.fillText('middle', 50, 75);
    
            canvasContext.setTextBaseline('bottom');
            canvasContext.fillText('bottom', 120, 75);
    
            canvasContext.setTextBaseline('normal');
            canvasContext.fillText('normal', 200, 75);
    
            canvasContext.draw();
        }
    });