文本标签控件 (SStatic)¶
文本标签是用于显示静态文本的基础控件,支持单行和多行文本显示,以及丰富的文本样式设置。
基本信息¶
- 类名:
SStatic
- 控件标签:
text
- 基类:
SWindow
- 功能:显示静态文本内容
属性说明¶
基本属性¶
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
text | string | - | text value |
interHeight | int | - | 行间距 |
wordBreak | int | - | 是否以字符为换行单位(0 |
文本样式¶
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
align | string | - | 水平对齐,可选值:left,center,right |
valign | string | - | 垂直对齐,可选值:top,middle,bottom |
colorText | color | - | 文本颜色 |
font | string | - | 文本字体 |
fontHover | string | - | Hover字体 |
fontPush | string | - | Push字体 |
fontDisable | string | - | Disable字体 |
colorTextHover | color | - | Hover颜色 |
colorTextPush | color | - | Push颜色 |
colorTextDisable | color | - | Disable颜色 |
dotted | bool | 0 | 省略号显示文本(是:1 |
外观属性¶
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
skin | string | - | 皮肤 |
colorBkgnd | color | - | 背景颜色,不指定skin时有效 |
colorBorder | color | - | 边框颜色,需要和margin一起使用 |
布局属性¶
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
margin | string | - | 边框大小,和colorBorder或者ncSkin配合使用,格式为左,上,右,下 |
padding | string | - | 内边距,和inset属性相同,格式为左,上,右,下 |
使用示例¶
基本文本¶
<text pos="10,10,200,30"
name="text_basic"
text="这是一个简单的文本标签"/>
多行文本¶
<text pos="10,40,200,100"
name="text_multiline"
multiLines="1"
interHeight="5"
wordBreak="1"
text="这是一个多行文本示例。\n第二行文本内容\n第三行文本内容"/>
自定义样式文本¶
<text pos="10,110,200,140"
name="text_styled"
text="带样式的文本"
font="face:微软雅黑,size:16,bold:1"
colorText="#FF0000"
align="center"
valign="middle"/>
带背景和边框的文本¶
<text pos="10,150,200,180"
name="text_bordered"
text="带边框的文本"
colorBkgnd="#F0F0F0"
colorBorder="#CCCCCC"
margin="2,2,2,2"
padding="5,5,5,5"/>
省略号显示文本¶
<text pos="10,190,200,210"
name="text_dotted"
text="这是一段很长的文本,当空间不足时会显示省略号"
dotted="1"/>
事件处理¶
文本标签控件支持以下事件:
事件名 | EventID | 说明 |
---|---|---|
EVT_MOUSE_HOVER | EventMouse::EventID | 鼠标悬停事件 |
EVT_MOUSE_LEAVE | EventMouse::EventID | 鼠标离开事件 |
EVT_SETFOCUS | EventSetFocus::EventID | 获得焦点事件 |
EVT_KILLFOCUS | EventKillFocus::EventID | 失去焦点事件 |
// 事件处理示例
EVENT_MAP_BEGIN()
EVENT_NAME_HANDLER(L"text_styled", EventMouse::EventID, OnTextMouseEvent)
EVENT_MAP_END()
void OnTextMouseEvent(IEvtArgs *pEvt)
{
// 处理鼠标事件
}
代码操作¶
// 查找文本控件
SStatic *pStatic = FindChildByName2<SStatic>(L"text_basic");
// 设置文本
pStatic->SetWindowText(L"新文本内容");
// 获取文本
SStringT strText = pStatic->GetWindowText();
// 设置文本颜色
pStatic->SetAttribute(L"colorText", L"#00FF00");
// 启用/禁用省略号显示
pStatic->SetAttribute(L"dotted", L"1");
最佳实践¶
- 文本换行:对于多行文本,合理使用 interHeight 和 wordBreak 属性
- 样式统一:通过 font 和 colorText 属性保持界面文本风格统一
- 空间利用:使用 dotted 属性处理文本过长的情况
- 对齐方式:根据布局需要设置合适的 align 和 valign 值
常见问题¶
Q: 多行文本不换行怎么办?¶
A: 确保设置了适当的换行符 \n
,或使用 wordBreak 属性控制换行行为。
Q: 文本显示不完整怎么办?¶
A: 检查控件尺寸是否足够,或使用 dotted 属性启用省略号显示。
Q: 文本样式不生效怎么办?¶
A: 确保 font 和 colorText 等属性设置正确,且没有被其他样式覆盖。
相关控件¶
- 编辑框(SEdit) - 可编辑文本控件
- 富文本编辑框(SRichEdit) - 富文本编辑控件
- 链接控件(SLink) - 可点击链接文本控件