我想知道如何在 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
对象应用了任何转换。
关于c++ - 在 SFML 中获取文本的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244928/