dev-zuo 技术日常
Github

canvas不支持文本换行怎么处理

这篇文章发布于 2020/07/06,归类于
标签:
canvas不支持文本换行怎么处理canvas文本换行canvas文本怎么换行

今天在stackoveflow里面搜索ctx.fill的问题时,查到了很多关于canvas ctx.fillText()绘制文本时不支持换行的问题,找到了一个比较好的答案

I'm afraid it is a limitation of Canvas' fillText. There is no multi-line support. Whats worse, there's no built-in way to measure line height, only width, making doing it yourself even harder!

一般解决思路是,根据 ctx.measureText('Hello').width 来看需要显示的文字是否需要换行,写一个for循环来处理

参考: