gpt4 book ai didi

c++ - 在 SFML 中获取文本的尺寸

转载 作者:可可西里 更新时间:2023-11-01 18:17:45 24 4
gpt4 key购买 nike

我想知道如何在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com