Add info on centering text

master
Jeffery Myers 3 years ago
parent
commit
015b67d0a1
1 changed files with 22 additions and 0 deletions
  1. +22
    -0
      Frequently-asked-Questions--Common-Questions.md

+ 22
- 0
Frequently-asked-Questions--Common-Questions.md

@ -68,4 +68,26 @@ This will convert a screen point into a world point for a camera. This will incl
This function gets a screen point for a world point, using zoom and scale. It is useful for computing the location of HUD elements that should not be scaled or rotated with the world view, such as player names, health bars, or other labels.
# How do I center text on the screen?
Raylib does not offer any text formatting functions, so you need to compute the starting point for all text that you draw. The starting point for text is always the upper left corner.
You can compute the center of the screen by dividing the screen width and hieght in half.
```
int screenCenterX = GetScreenWidth() / 2;
int screenCenterY = GetScreenHeight() / 2;
```
Next you need to compute how large the text is, and offset the center by half the text size.
```
const char* text = "Congrats! You created your first window!";
int fontSize = 20;
int textWidth = MeasureText(text, fontSize);
int textStartX = screenCenterX - textWidth / 2;
int textStartY = screenCenterY - fontSize / 2;
DrawText(text, textStartX, textStartY, fontSize, LIGHTGRAY);
```
`MeasureText` only measures the width of text, but takes fewer arguments. It is often acceptable to just use the font size as the total text height, but for some fonts, this may not be accurate. `MeasureTextEx` will measure both height and width of text, but does take more arguments. For this reason it is used less often.

Loading…
Cancel
Save