【问题标题】:Getting dimensions of text in SFML在 SFML 中获取文本的尺寸
【发布时间】:2012-10-26 01:05:41
【问题描述】:

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

有人知道吗?

谢谢:)

【问题讨论】:

    标签: c++ sfml


    【解决方案1】:

    您可以使用 getGlobalBounds() 获取变换后的大小/坐标(旋转、缩放、移动...)。 否则就是 getLocalBounds()。

    文档:http://www.sfml-dev.org/documentation/2.3.1/classsf_1_1Text.php

    【讨论】:

      【解决方案2】:

      查看文档似乎是函数 getLocalBounds 可能对您有用。该行将是:

      float width = text.getLocalBounds().width;
      

      我不确定sf::Text 对象是否会在边界矩形的末端添加任何填充。

      或者,您可以使用findCharacterPos 类似的东西:

      float width = text.findCharacterPos(numChars - 1).x - text.findCharacterPos(0).x;
      

      其中numCharstext 对象的字符串中的字符数。但是,由于findCharacterPos 会返回全局坐标,使用getLocalBounds 可能更方便,这样您就不必担心您的text 对象是否应用了任何变换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-21
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多