DxGetTextSize: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
Fernando187 (talk | contribs) (Remove obsolete Requirements section) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Client function}} | {{Client function}} | ||
{{New items|3. | {{New items|3.0158|1.5.7|This function retrieves the theoretical width and height (in pixels) of a certain piece of text, if it were to be drawn using [[dxDrawText]].|20447}} | ||
'''NOTE:''' This function already takes the client's screen resolution into account. | '''NOTE:''' This function already takes the client's screen resolution into account. | ||
Line 7: | Line 7: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
float, float dxGetTextSize ( string text, | float, float dxGetTextSize ( string text [, float width = 0, float scaleXY = 1.0 [, float scaleY = 1.0 ], mixed font = "default", bool wordBreak = false, bool colorCoded = false] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP|This syntax requires you to ignore the font argument above| [[Element/DX font|font]]:getSize}} | {{OOP|This syntax requires you to ignore the font argument above| [[Element/DX font|font]]:getSize}} | ||
Line 16: | Line 16: | ||
{{OptionalArg}} | {{OptionalArg}} | ||
* '''width:''' The width of the text. Use with ''wordBreak = true''. | * '''width:''' The width of the text. Use with ''wordBreak = true''. | ||
* ''' | * '''scaleX:''' The scale of the text. Scale can also be inputted as a [[Vector2]]. | ||
* '''scaleY:''' The scale of the text. | |||
* '''font:''' Either a custom [[DX font]] element or the name of a built-in dx font: | * '''font:''' Either a custom [[DX font]] element or the name of a built-in dx font: | ||
{{DxFonts}} | {{DxFonts}} | ||
* '''wordBreak:''' If set to ''true'', the text will wrap to a new line whenever it reaches the right side of the bounding box. If ''false'', the text will always be completely on one line. | * '''wordBreak:''' If set to ''true'', the text will wrap to a new line whenever it reaches the right side of the bounding box. If ''false'', the text will always be completely on one line. | ||
* ''' | * '''colorCoded:''' Should we exclude color codes from the width? False will include the hex in the length. | ||
===Returns=== | ===Returns=== | ||
Returns two floats representing the width and height of the text | Returns two floats representing the width and height of the text in pixels. | ||
==Example== | ==Example== | ||
This example draws a text with black background at the bottom right corner of the screen. | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- | local screenWidth, screenHeight = guiGetScreenSize() | ||
local message = "Incredibly huuuuuuuge message" | |||
local messageOffset = 32 | |||
local messagePadding = 16 | |||
local messageWidth = 256 | |||
function renderMessage() | |||
local textWidth, textHeight = dxGetTextSize(message, messageWidth, 2, "default", true) | |||
local x = screenWidth - textWidth - messageOffset | |||
local y = screenHeight - textHeight - messageOffset | |||
dxDrawRectangle(x - messagePadding, y - messagePadding, textWidth + messagePadding * 2, textHeight + messagePadding * 2, 0x80000000) -- draw background | |||
dxDrawText(message, x, y, x + textWidth, y + textHeight, 0xFFFFFFFF, 2, "default", "left", "top", false, true) | |||
end | |||
addEventHandler("onClientRender", root, renderMessage) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:DxGetTextSize.png|frame|none|Example]] | |||
==See Also== | ==See Also== | ||
{{Drawing_functions}} | {{Drawing_functions}} |
Latest revision as of 17:19, 7 November 2024
This function retrieves the theoretical width and height (in pixels) of a certain piece of text, if it were to be drawn using dxDrawText.
NOTE: This function already takes the client's screen resolution into account.
Syntax
float, float dxGetTextSize ( string text [, float width = 0, float scaleXY = 1.0 [, float scaleY = 1.0 ], mixed font = "default", bool wordBreak = false, bool colorCoded = false] )
OOP Syntax Help! I don't understand this!
- Note: This syntax requires you to ignore the font argument above
- Method: font:getSize(...)
Required Arguments
- text: A string representing the text for which you wish to retrieve with width for.
Optional Arguments
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.
- width: The width of the text. Use with wordBreak = true.
- scaleX: The scale of the text. Scale can also be inputted as a Vector2.
- scaleY: The scale of the text.
- font: Either a custom DX font element or the name of a built-in dx font:
- "default": Tahoma
- "default-bold": Tahoma Bold
- "clear": Verdana
- "arial": Arial
- "sans": Microsoft Sans Serif
- "pricedown": Pricedown (GTA's theme text)
- "bankgothic": Bank Gothic Medium
- "diploma": Diploma Regular
- "beckett": Beckett Regular
- "unifont": Unifont
- wordBreak: If set to true, the text will wrap to a new line whenever it reaches the right side of the bounding box. If false, the text will always be completely on one line.
- colorCoded: Should we exclude color codes from the width? False will include the hex in the length.
Returns
Returns two floats representing the width and height of the text in pixels.
Example
This example draws a text with black background at the bottom right corner of the screen.
local screenWidth, screenHeight = guiGetScreenSize() local message = "Incredibly huuuuuuuge message" local messageOffset = 32 local messagePadding = 16 local messageWidth = 256 function renderMessage() local textWidth, textHeight = dxGetTextSize(message, messageWidth, 2, "default", true) local x = screenWidth - textWidth - messageOffset local y = screenHeight - textHeight - messageOffset dxDrawRectangle(x - messagePadding, y - messagePadding, textWidth + messagePadding * 2, textHeight + messagePadding * 2, 0x80000000) -- draw background dxDrawText(message, x, y, x + textWidth, y + textHeight, 0xFFFFFFFF, 2, "default", "left", "top", false, true) end addEventHandler("onClientRender", root, renderMessage)
See Also
- dxConvertPixels
- dxCreateFont
- dxCreateRenderTarget
- dxCreateScreenSource
- dxCreateShader
- dxCreateTexture
- dxDrawCircle
- dxDrawImage
- dxDrawImageSection
- dxDrawLine
- dxDrawLine3D
- dxDrawMaterialLine3D
- dxDrawMaterialPrimitive
- dxDrawMaterialPrimitive3D
- dxDrawMaterialSectionLine3D
- dxDrawPrimitive
- dxDrawPrimitive3D
- dxDrawRectangle
- dxDrawText
- dxDrawWiredSphere
- dxGetBlendMode
- dxGetFontHeight
- dxGetMaterialSize
- dxGetPixelColor
- dxGetPixelsSize
- dxGetPixelsFormat
- dxGetStatus
- dxGetTextSize
- dxGetTextWidth
- dxGetTexturePixels
- dxIsAspectRatioAdjustmentEnabled
- dxSetAspectRatioAdjustmentEnabled
- dxSetBlendMode
- dxSetPixelColor
- dxSetRenderTarget
- dxSetShaderValue
- dxSetShaderTessellation
- dxSetShaderTransform
- dxSetTestMode
- dxSetTextureEdge
- dxSetTexturePixels
- dxUpdateScreenSource