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

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

本文关键字:getPosition getLocalBounds 返回 类型 sf Text 为什么 对象      更新时间:2023-10-16

我想在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)

有什么想法吗?

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