如何对齐文本通过SpriteBatch.DrawString画?文本、SpriteBatch、DrawString

2023-09-11 07:45:08 作者:╭⌒安颜〆

有没有一种简单的方法来对齐文本的权利和中心(而不是默认的左)?

Is there an easy way to align text to the right and center (instead of default left)?

推荐答案

第一步是通过测量字符串SpriteFont.MeasureString().

The first step is to measure the string using SpriteFont.MeasureString().

然后,例如,如果你想将它画到一定点的左边,而不是在右边的是默认的,那么你就需要从文本绘制原点减去测量的X宽。如果你希望它为中心,那么你可以用一半的测量等。

Then, for example if you want to draw it to the left of a certain point, instead of to the right as is the default, then you need to subtract the X width of the measurement from the text drawing origin. If you want it to be centered, then you can use half the measurement, etc.