TextWidget API¶
TextWidget(widget_id=None)
¶
Bases: BaseWidget
TextWidget 是用于显示各种类型文本内容的组件,支持多种预定义文本类型和丰富的样式配置.
这个Widget支持多种文本类型(标题、正文、说明文字、章节标题等), 并提供丰富的样式设置选项.章节标题会自动添加编号.
主要功能: - 支持多种预定义文本类型 - 自定义字体、颜色、对齐方式等样式 - 自动章节编号 - 多行文本支持 - 响应式设计
Attributes:
Name | Type | Description |
---|---|---|
_content |
str
|
文本内容. |
_text_type |
TextType
|
文本类型. |
_font_size |
str
|
字体大小. |
_align |
TextAlign
|
对齐方式. |
_color |
str
|
文本颜色. |
_line_height |
str
|
行高. |
_font_weight |
str
|
字体粗细. |
_font_family |
str
|
字体族. |
_margin |
str
|
外边距. |
_max_width |
Optional[str]
|
最大宽度. |
_section_number |
Optional[str]
|
章节编号. |
Examples:
from email_widget.widgets import TextWidget
from email_widget.core.enums import TextType, TextAlign
# 基本用法
text = TextWidget().set_content("Hello World")
# 链式调用
title = (TextWidget() .set_content("重要标题") .set_type(TextType.TITLE_LARGE) .set_color("#0078d4") .set_align(TextAlign.CENTER))
# 章节标题(自动编号)
section = TextWidget().set_content("数据分析").set_type(TextType.SECTION_H2)
初始化TextWidget.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
widget_id
|
Optional[str]
|
可选的Widget ID. |
None
|
Attributes¶
align
property
¶
获取当前对齐方式.
Returns:
Name | Type | Description |
---|---|---|
TextAlign |
TextAlign
|
当前的对齐方式枚举值. |
color
property
¶
获取当前文本颜色.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
当前的文本颜色CSS值. |
content
property
¶
获取当前文本内容.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
当前的文本内容. |
font_size
property
¶
获取当前字体大小.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
当前的字体大小CSS值. |
Functions¶
get_template_context()
¶
获取模板渲染所需的上下文数据.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
模板上下文数据字典 |
reset_section_numbers()
staticmethod
¶
set_align(align)
¶
设置文本对齐方式.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
align
|
TextAlign
|
对齐方式枚举值. |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_bold(bold=True)
¶
设置是否为粗体.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bold
|
bool
|
是否为粗体,默认为True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_color(color)
¶
设置文本颜色.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color
|
str
|
CSS颜色值(如 "#ff0000", "red", "rgb(255,0,0)"). |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Raises:
Type | Description |
---|---|
ValueError
|
当颜色格式无效时. |
Examples:
set_content(content)
¶
设置文本内容,支持多行文本(使用分隔).
Args:
content (str): 文本内容.
Returns:
TextWidget: 支持链式调用.
Raises:
ValueError: 当内容为空字符串时.
Examples:
>>> widget = TextWidget().set_content("Hello World")
>>> # 多行文本
>>> widget = TextWidget().set_content("第一行
第二行 第三行")
set_font_family(family)
¶
设置字体族.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
family
|
str
|
CSS字体族字符串. |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_font_size(size)
¶
设置字体大小.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
CSS字体大小值(如 "16px", "1.2em", "120%"). |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Raises:
Type | Description |
---|---|
ValueError
|
当尺寸格式无效时. |
Examples:
set_font_weight(weight)
¶
设置字体粗细.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight
|
str
|
CSS字体粗细值(如 "normal", "bold", "600"). |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_italic(italic=True)
¶
设置是否为斜体.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
italic
|
bool
|
是否为斜体,默认为True |
True
|
Returns:
Type | Description |
---|---|
TextWidget
|
返回self以支持链式调用 |
Note
当前版本暂未实现斜体功能,预留接口
Examples:
set_line_height(height)
¶
设置行高.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
height
|
str
|
CSS行高值(如 "1.5", "24px", "150%"). |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_margin(margin)
¶
设置外边距.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
margin
|
str
|
CSS外边距值(如 "16px 0", "10px", "1em 2em"). |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_max_width(max_width)
¶
设置最大宽度.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_width
|
str
|
CSS最大宽度值(如 "600px", "80%", "50em"). |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples:
set_type(text_type)
¶
设置文本类型,不同类型会应用不同的预设样式.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text_type
|
TextType
|
文本类型枚举值. |
required |
Returns:
Name | Type | Description |
---|---|---|
TextWidget |
TextWidget
|
支持链式调用. |
Examples: