Label 是最基础的文本显示控件,支持字体、颜色、对齐、换行、下划线与段落间距设置。
Label 用于显示静态文本,是 UI 中出现频率最高的控件:
• 支持多行文本、自动换行
• 可设置字体、颜色、对齐方式
• 支持下划线装饰和段落间距
• 禁用态自动切换 DisabledTextColor
| 函数 | 说明 |
|---|---|
int xuiLabelCreate(xui_context pCtx, xui_widget* ppW, const xui_label_desc_t* pDesc) | 创建标签 |
int xuiLabelSetText(xui_widget pW, const char* sText) | 设置文本 |
const char* xuiLabelGetText(xui_widget pW) | 获取文本 |
int xuiLabelSetFont(xui_widget pW, xui_font pFont) | 设置字体 |
int xuiLabelSetTextColor(xui_widget pW, uint32_t iColor) | 设置文本颜色 |
int xuiLabelSetDisabledTextColor(xui_widget pW, uint32_t iColor) | 禁用态颜色 |
int xuiLabelSetTextFlags(xui_widget pW, uint32_t iFlags) | 对齐标志 |
int xuiLabelSetWrapMode(xui_widget pW, int iMode) | 换行模式 |
int xuiLabelSetUnderline(xui_widget pW, int bUnderline) | 下划线 |
int xuiLabelSetLineGap(xui_widget pW, float fGap) | 行间距 |
int xuiLabelSetParagraphGap(xui_widget pW, float fGap) | 段落间距 |
gcc -O2 -I. -o ch131.exe ch131_main.c src\xui_*.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch131.exe
xui_label_desc_t desc = { 0 };
desc.sText = "欢迎使用 XGE 引擎\n第二行文本";
xui_widget pLabel;
xuiLabelCreate(pCtx, &pLabel, &desc);
// 设置字体和颜色
xuiLabelSetFont(pLabel, pMyFont);
xuiLabelSetTextColor(pLabel, 0xFFE0E0E0);
// 自动换行 + 行间距
xuiLabelSetWrapMode(pLabel, 1);
xuiLabelSetLineGap(pLabel, 4.0f);
xuiLabelSetParagraphGap(pLabel, 8.0f);
// 添加到父容器
xuiWidgetAddChild(pParent, pLabel);将以下代码保存为 ch131_main.c,编译后运行即可看到 Label 多行文本显示:
#include "xge.h"
#include "xui.h"
#include <stdio.h>
static xui_context g_pCtx;
static xui_widget g_pLabel;
static int g_nFrame = 0;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 每 60 帧更新 FPS 显示
if (++g_nFrame % 60 == 0) {
char buf[64];
sprintf(buf, "FPS: %.1f\nFrame: %d",
1.0f / xgeGetDelta(), g_nFrame);
xuiLabelSetText(g_pLabel, buf);
}
xuiUpdate(g_pCtx, xgeGetDelta());
xuiLayout(g_pCtx);
xuiRender(g_pCtx, NULL, NULL, 0);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch131 - Label";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xuiCreate(&g_pCtx);
extern const xui_proxy_t tXgeProxy;
xui_proxy_t tXgeProxy = xuiProxyXge();
xuiSetProxy(g_pCtx, &tXgeProxy);
xuiSetViewportSize(g_pCtx, 800, 600);
// 创建多行标签
xui_label_desc_t desc = { 0 };
desc.sText = "XGE Engine Tutorial\nChapter 131 - Label";
xuiLabelCreate(g_pCtx, &g_pLabel, &desc);
xuiLabelSetTextColor(g_pLabel, 0xFFE0E0E0);
xuiLabelSetWrapMode(g_pLabel, 1);
xuiLabelSetLineGap(g_pLabel, 4.0f);
xuiWidgetSetRect(g_pLabel,
(xui_rect_t){ 50, 50, 400, 200 });
xgeRun(onFrame, NULL);
xuiDestroy(g_pCtx);
xgeUnit();
return 0;
}