Skip to content
Kiminaze edited this page May 15, 2021 · 6 revisions

Working with Text in the ContextMenu

All items in the ContextMenu use this Text to display their title. For the menu itself you normally never need to create this manually (unlike e.g. a new Color). You can however use this class to draw additional text on the screen (e.g. for custom HUD elements).

Creating a new text

To create a new text you can use the Text(title, color) function. This will result in a new Text being created.

local myText1 = Text("Hello world!", Colors.Red)

-- results in white text color
local myText2 = Text("Goodbye!")

Text variables

Text provides 4 distinct variables. These are also the only things you would ever want to change.

local myText = Text("Hello world!")

-- prints the actual text
print(myText.title)

-- change the position on the screen
-- vector2(0.0, 0.0) is left top corner of the safezone
-- vector2(1.0, 1.0) is right bottom corner of the safezone
myText.position = vector2(0.5, 0.5)

-- change the scaling of the text
myText.scale = vector2(0.1, 0.1)

-- change the color of the text
myText.color = Colors.Green

Drawing the Text on the screen

Drawing text must be done every frame and can be achieved using the Draw() function of the Text. Example:

Citizen.CreateThread(function()
    local myText = Text("Hello world!")
    
    while (true) do
        Citizen.Wait(0)
        
        myText:Draw()
    end
end)

Getting the width of a Text

You can retrieve the current width of a Text using the GetWidth() function of the Text.

local myText = Text("Hello world!")

local textWidth = myText:GetWidth()

Clone this wiki locally