我想知道如何在 SFML 中获得文本的尺寸?
我试着这样做:
sf::Text text("Hello SFML", font, 50);
// using text.getRect()
// i also tried getScale() & getSize()
// neither are correct
text.setPosition( window.getSize().y/2 - text.getRect().y,50 );
有人知道吗 ?
谢谢 :)
查看文档,该功能似乎
getLocalBounds
对您有用。该行将是:
float width = text.getLocalBounds().width;
我不确定该sf::Text
对象是否会在边界矩形的末端添加任何填充。
或者,您可以使用以下内容findCharacterPos
:
float width = text.findCharacterPos(numChars - 1).x - text.findCharacterPos(0).x;
其中numChars
是text
对象字符串中的字符数。但是,由于findCharacterPos
会返回全局坐标,使用起来可能更方便getLocalBounds
,这样您就不必担心您的text
对象是否应用了任何变换。
您可以使用 getGlobalBounds() 获取变换后的大小/坐标(旋转、缩放、移动...)。否则它是 getLocalBounds()。
文档:http ://www.sfml-dev.org/documentation/2.3.1/classsf_1_1Text.php