本文介绍了斯基亚-measureText()返回的值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Skia measureText()函数测量文本时遇到问题.返回的值不正确.

I have a problem measuring text using skia measureText() function.The value returned is inaccurate.

SkPaint *skPaint = new SkPaint();

SkTypeface* myFont = SkTypeface::CreateFromName("Impact", SkTypeface::kNormal);

skPaint->setTypeface(myFont);
skPaint->setAntiAlias(true);
skPaint->setTextAlign(SkPaint::kLeft_Align);
skPaint->setTextEncoding(SkPaint::kUTF16_TextEncoding);
skPaint->setTextSize(SkIntToScalar(120));
skPaint->setColor(0xff000001);
canvas->drawText(text, length,  SkIntToScalar(x) ,  SkIntToScalar(y) , *skPaint);
SkScalar width = skPaint->measureText(text, length);

measureText()返回的宽度为451.

The width returned by measureText() is 451.

我通过照片编辑器应用检查了生成的位图文本,实际宽度只有438.

I checked the generated bitmap text via a photo editor app, the actual width is only 438.

是否有任何关于在SKIA中获取准确的文本宽度的想法?

Any thoughts on getting the accurate width of text in SKIA?

谢谢!

推荐答案

我相信您要匹配的内容将来自界限"

I believe what you are trying to match will come from "bounds"

SkRect bounds;
SkScalar textWidth = paint.measureText("some", 4, &bounds);

这是适合给定文本的最小矩形,而textWidth稍大于该矩形.

which is a minimum rectangle to fit a given text, whereas textWidth is slightly larger than that.

这篇关于斯基亚-measureText()返回的值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 16:27