矩形文本居中无效
Centering text on a rectangle not working
我有一个sf::RectangleShape inputFieldRect_
和sf::Text inputFieldText_
,我想把文本放在矩形的中心,因为sf:: text的尺寸会变化,因为它的字符串会有不同的长度。
用inputFieldText_.setString(s);
设置inputFieldText_
的字符串后(s
是用户输入的std::string
),我正在调整它的位置,使它仍然在inputFieldRect_
的中心。
这是我的代码但是文本偏左偏下,所以没有完全居中
sf::FloatRect rectBounds = inputFieldRect_.getGlobalBounds();
sf::FloatRect textBounds = inputFieldText_.getGlobalBounds();
inputFieldText_.setPosition(
rectBounds.left + (rectBounds.width / 2) - (textBounds.width / 2),
rectBounds.top + (rectBounds.height / 2) - (textBounds.height / 2)
);
有人能帮帮我吗?
如果文本的数量非常小,我建议您查看文本的绘制点在哪里:通常从文本基线(Y)和从第一个字符的逻辑初始X绘制文本。合乎逻辑,因为这可能比你预期的更正确。**
如果这没有帮助,调查是否应该使用LocalBounds可能是有用的:有时在框架中,差异并不像看起来那么简单。
**这个区别被称为左轴承(是的,也有右轴承)。详见本页备注:http://msdn.microsoft.com/en-us/library/system.windows.media.glyphrundrawing(v=vs.110).aspx
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 无法访问嵌套类.类的使用无效
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 将文本文件读取为 const char* 有时有效,有时无效
- 通过fscanf()获取文本文件中的所有字符将返回无效的字符计数
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 读取文本文件会导致缓冲区末尾的字符无效
- Qt - 无效使用不完整的类型 'class QScrollBar' - 为文本编辑小部件添加水平滚动条
- 矩形文本居中无效