Skip to content

Class "Font"⚓︎

Info

这个类是用来加载和渲染字体的。它可以通过以下方式访问:

Example Code
1
    local myFont = Font()

Constructors⚓︎

Font ()⚓︎

Font Font ( )⚓︎

"Font" 类的构造函数.

Example Code

Example usage.

1
2
3
    local f = Font() -- init font object
    f:Load("font/terminus.fnt") -- load a font into the font object
    f:DrawString("Hello World!",60,50,KColor(1,1,1,1),0,true) -- render string with loaded font on position 60x50y

Functions⚓︎

Draw·String ()⚓︎

void DrawString ( string String, float PositionX, float PositionY, KColor RenderColor, int BoxWidth = 0, boolean Center = false )⚓︎

void DrawString ( string String, float PositionX, float PositionY, float sizeX, float sizeY, KColor RenderColor, FontRenderSettings settings )⚓︎

将 UTF8 转换为 UTF16,然后在屏幕上绘制字符串.

BoxWidthCenter 变量可以用于对齐文本。需要注意以下几点:

  • 如果 BoxWidth 为零,文本将左对齐,Center 参数会被忽略.
  • 如果 BoxWidth 不为零,且 Center 参数为 false,则文本会在 BoxWidth 范围内右对齐.
  • 如果 BoxWidth 不为零,且 Center 参数为 true,则文本会在 BoxWidth 范围内居中对齐.
Bug

如果调用此函数时 StringRenderColor 参数为 nil,游戏会崩溃.

Example Code

Example usage.

1
2
3
4
5
6
    -- In an initialization function:
    local f = Font() -- init font object
    f:Load("font/terminus.fnt") -- load a font into the font object

    -- In a render function on every frame:
    f:DrawString("Hello World!",60,50,KColor(1,1,1,1),0,true) -- render string with loaded font on position (60, 50)

Draw·String·Scaled ()⚓︎

void DrawStringScaled ( string String, float PositionX, float PositionY, float ScaleX, float ScaleY, KColor RenderColor, int BoxWidth = 0, boolean Center = false )⚓︎

将 UTF8 转换为 UTF16,然后在屏幕上绘制缩放后的字符串.

Bug

如果调用此函数时 StringRenderColor 参数为 nil ,游戏会崩溃.

Example Code

Example usage.

1
2
3
    local f = Font() -- init font object
    f:Load("font/terminus.fnt") -- load a font into the font object
    f:DrawStringScaled("Hello World!",60,50,0.5,0.5,KColor(1,1,1,1),0,true) -- render string with loaded font on position 60x50y

Draw·String·Scaled·UTF8 ()⚓︎

void DrawStringScaledUTF8 ( string String, float PositionX, float PositionY, float ScaleX, float ScaleY, KColor RenderColor, int BoxWidth = 0, boolean Center = false )⚓︎

在屏幕上绘制缩放后的 Unicode 文本字符串.

Bug

如果调用此函数时 StringRenderColor 参数为 nil ,游戏会崩溃.

Example Code

Example usage.

1
2
3
    local f = Font() -- init font object
    f:Load("font/terminus.fnt") -- load a font into the font object
    f:DrawStringScaledUTF8("Hello World!",60,50,0.5,0.5,KColor(1,1,1,1),0,true) -- render string with loaded font on position 60x50y

Draw·String·UTF8 ()⚓︎

void DrawStringUTF8 ( string String, float PositionX, float PositionY, KColor RenderColor, int BoxWidth = 0, boolean Center = false )⚓︎

在屏幕上绘制一串 Unicode 文本.

BoxWidthCenter 参数可用于对齐文本,注意如下:

  • 如果 BoxWidth 为零,文本将左对齐,Center 参数会被忽略.
  • 如果 BoxWidth 不为零,且 Center 参数为 false,则文本会在 BoxWidth 范围内右对齐.
  • 如果 BoxWidth 不为零,且 Center 参数为 true,则文本会在 BoxWidth 范围内居中对齐.
Bug

如果调用此函数时 StringRenderColor 参数为 nil,游戏会崩溃。

Example Code

Example usage.

1
2
3
    local f = Font() -- init font object
    f:Load("font/terminus.fnt") -- load a font into the font object
    f:DrawStringUTF8("Hello World!",60,50,KColor(1,1,1,1),0,true) -- render string with loaded font on position 60x50y

Get·Baseline·Height ()⚓︎

int GetBaselineHeight ( )⚓︎

返回从行的绝对顶部到字符基线的像素数(基准高度).


Get·Character·Width ()⚓︎

int GetCharacterWidth ( char Character )⚓︎

返回指定字符的像素宽度.


Get·Line·Height ()⚓︎

int GetLineHeight ( )⚓︎

返回每行文本之间的像素距离(行高度).


Get·String·Width ()⚓︎

int GetStringWidth ( string String )⚓︎

将字符串从 UTF8 转换为 UTF16,并返回该字符串的像素宽度.

Bug

如果调用此函数时参数为 nil,游戏会崩溃.


Get·String·Width·UTF8 ()⚓︎

int GetStringWidthUTF8 ( string String )⚓︎

返回 Unicode 文本的像素宽度.


Is·Loaded ()⚓︎

boolean IsLoaded ( )⚓︎

返回字体是否已被加载.


Load ()⚓︎

void Load ( string FilePath )⚓︎

加载字体。要检查字体是否实际加载成功,可随后调用 IsLoaded() 方法.

Bug

加载自定义字体时,传递给此函数的路径实际上是相对于游戏的 resources 文件夹,而不是你的 mod 的 resources 文件夹,这与 Sprite:Load() 等函数的行为不一致。你可以用如下代码加载自定义字体:

1
2
3
4
5
local MOD_FOLDER_NAME = "mymod" -- change to your mod's directory. note that this can be different depending on how the mod was downloaded: if downloaded from the steam workshop, it will have an underscore with the mod's steam id at the end (e.g. "mymod_12345")
local CUSTOM_FONT_FILE_PATH = "font/mycoolfont.fnt" -- relative to your "resources" folder

local customFont = Font()
customFont:Load("mods/" .. MOD_FOLDER_NAME .. "/resources/" .. CUSTOM_FONT_FILE_PATH)
Example Code

Example usage.

1
2
3
    local f = Font() -- init font object
    f:Load("font/terminus.fnt") -- load a font into the font object
    f:DrawString("Hello World!", 60, 50, KColor.White, 0, true) -- render string with loaded font on position 60x50y

Set·Missing·Character ()⚓︎

void SetMissingCharacter ( char MissingCharacter )⚓︎

设置当字体遇到缺失字符时所使用的字符.


Unload ()⚓︎

void Unload ( )⚓︎

从内存中卸载字体.



Last update: February 14, 2026