GetTextWidth
Returns the width of a line of text when drawn with this font. Use this to center text or calculate background dimensions.
Syntax
- int GetTextWidth(const std::string& text)
Parameters
- text: the text to calculate the width of.
Returns
Returns the width of the specified text.
Example
#include "Leadwerks.h"
using namespace Leadwerks;
int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
//Load a font
Font* font = Font::Load("Fonts/arial.ttf", 36);
context->SetFont(font);
while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;
context->SetColor(0, 0, 0, 0);
context->Clear();
context->SetColor(1, 1, 1, 0);
//Display some centered text
std::string text = "Leadwerks";
int screenwidth = context->GetWidth();
int screenheight = context->GetHeight();
int x = (screenwidth - font->GetTextWidth(text)) / 2;
int y = (screenheight - font->GetHeight()) / 2;
context->SetBlendMode(Blend::Alpha);
context->DrawText(text, x, y);
context->SetBlendMode(Blend::Solid);
context->Sync();
}
return 0;
}