为什么 sf::Text returns 类型的对象不同 getPosition().y 和 getLocalBounds().top?

Why object of type sf::Text returns different getPosition().y and getLocalBounds().top?

我想在SFML 2.4.1中精确设置sf::Text的位置,但是设置字体后位置不对

#include <SFML/Graphics.hpp>
#include <iostream>
#include <stdlib.h>

int main()
{
    sf::Text text;
    text.setCharacterSize(24);
    sf::Font font;
    font.loadFromFile("Font.ttf"); //without loading any font, everything's correct
    text.setFont(font);
    text.setString("A String");
    text.setPosition(0, 61);
    std::cout << text.getOrigin().y;
    std::cout << text.getPosition().y;
    std::cout << text.getGlobalBounds().top;
    std::cout << text.getLocalBounds().top;
    if (text.getLocalBounds().top != text.getPosition().y) return -1;
    return 0;
}

我试过更改原点,但没用。

text.setOrigin(0, text.getGlobalBounds().height/2.f); //height is correct and it matches with what displays on the screen(draw code is unnecessary)

有什么想法吗?

发生这种情况是因为第一行垂直对齐最高字符的高度(即使它不在字符串中)。这是为了保持字符串的顶部稳定,即使您在第一行添加更高的字符也是如此。感谢Laurent.