【问题标题】:Align text in the center of a RectangleShape在 RectangleShape 的中心对齐文本
【发布时间】:2019-09-02 14:11:43
【问题描述】:

我目前正在尝试用 SFML/C++ 编写一个按钮类,但是在对齐按钮中间的文本时遇到了问题。

我一直在使用标准对齐公式(按钮 x 位置 / 2) - (文本宽度 / 2)和(按钮 y 位置 / 2) - (文本高度 / 2)

void setPosition(sf::Vector2f point) {
        button.setPosition(point);
        float xPos = (point.x / 2) - (text.getLocalBounds().width / 2);
        float yPos = (point.y / 2) - (text.getLocalBounds().height / 2);
        text.setPosition(xPos, yPos);
    }

这段代码的结果是文本向上和向左移动,并且肯定不在中心。我确信这与 SFML 如何几乎相反地设置它们的坐标有关,但我不确定。任何帮助表示赞赏!

【问题讨论】:

    标签: c++ sfml text-alignment


    【解决方案1】:

    这个公式似乎对我有用:

    void setPosition(sf::Vector2f point) {
            button.setPosition(point);
            float xPos = (point.x + btnWidth / 2) - (text.getLocalBounds().width / 2);
            float yPos = (point.y + btnHeight / 2) - (text.getLocalBounds().height / 2);
            text.setPosition(xPos, yPos);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-23
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      相关资源
      最近更新 更多