文本¶
完全限定名: manim.mobject.text.text_mobject.Text
- class Text(text, fill_opacity=1.0, stroke_width=0, *, color=ManimColor('#FFFFFF'), font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', t2c=None, t2f=None, t2g=None, t2s=None, t2w=None, gradient=None, tab_width=4, warn_missing_font=True, height=None, width=None, should_center=True, disable_ligatures=False, use_svg_cache=False, **kwargs)[source]¶
基类:
SVGMobject
使用 Pango 渲染显示(非 LaTeX)文本。
Text 对象表现为一个
VGroup
类的可迭代对象,包含给定文本中的所有字符。特别是,它支持切片操作。- 参数:
text (str) – 需要被创建为 mobject 的文本。
font (str) – 用于渲染文本的字体族。这可以是系统字体,也可以是通过 register_font() 加载的字体。请注意,字体族名称可能因操作系统而异。
warn_missing_font (bool) – 如果为 True(默认),如果字体在 manimpango.list_fonts() 返回的(区分大小写的)字体列表中不存在,Manim 将发出警告。
fill_opacity (float)
stroke_width (浮点数)
color (ParsableManimColor | None)
font_size (float)
line_spacing (float)
slant (str)
weight (str)
t2c (dict[str, str])
t2f (dict[str, str])
t2g (dict[str, tuple])
t2s (dict[str, str])
t2w (dict[str, str])
gradient (tuple)
tab_width (int)
高度 (浮点数)
宽度 (浮点数)
should_center (bool)
disable_ligatures (bool)
use_svg_cache (bool)
- 返回:
类 mobject 的
VGroup
。- 返回类型:
示例
示例: Example1Text ¶
from manim import * class Example1Text(Scene): def construct(self): text = Text('Hello world').scale(3) self.add(text)
class Example1Text(Scene): def construct(self): text = Text('Hello world').scale(3) self.add(text)
示例: TextColorExample ¶
from manim import * class TextColorExample(Scene): def construct(self): text1 = Text('Hello world', color=BLUE).scale(3) text2 = Text('Hello world', gradient=(BLUE, GREEN)).scale(3).next_to(text1, DOWN) self.add(text1, text2)
class TextColorExample(Scene): def construct(self): text1 = Text('Hello world', color=BLUE).scale(3) text2 = Text('Hello world', gradient=(BLUE, GREEN)).scale(3).next_to(text1, DOWN) self.add(text1, text2)
示例: TextItalicAndBoldExample ¶
from manim import * class TextItalicAndBoldExample(Scene): def construct(self): text1 = Text("Hello world", slant=ITALIC) text2 = Text("Hello world", t2s={'world':ITALIC}) text3 = Text("Hello world", weight=BOLD) text4 = Text("Hello world", t2w={'world':BOLD}) text5 = Text("Hello world", t2c={'o':YELLOW}, disable_ligatures=True) text6 = Text( "Visit us at docs.manim.community", t2c={"docs.manim.community": YELLOW}, disable_ligatures=True, ) text6.scale(1.3).shift(DOWN) self.add(text1, text2, text3, text4, text5 , text6) Group(*self.mobjects).arrange(DOWN, buff=.8).set(height=config.frame_height-LARGE_BUFF)
class TextItalicAndBoldExample(Scene): def construct(self): text1 = Text("Hello world", slant=ITALIC) text2 = Text("Hello world", t2s={'world':ITALIC}) text3 = Text("Hello world", weight=BOLD) text4 = Text("Hello world", t2w={'world':BOLD}) text5 = Text("Hello world", t2c={'o':YELLOW}, disable_ligatures=True) text6 = Text( "Visit us at docs.manim.community", t2c={"docs.manim.community": YELLOW}, disable_ligatures=True, ) text6.scale(1.3).shift(DOWN) self.add(text1, text2, text3, text4, text5 , text6) Group(*self.mobjects).arrange(DOWN, buff=.8).set(height=config.frame_height-LARGE_BUFF)
示例: TextMoreCustomization ¶
from manim import * class TextMoreCustomization(Scene): def construct(self): text1 = Text( 'Google', t2c={'[:1]': '#3174f0', '[1:2]': '#e53125', '[2:3]': '#fbb003', '[3:4]': '#3174f0', '[4:5]': '#269a43', '[5:]': '#e53125'}, font_size=58).scale(3) self.add(text1)
class TextMoreCustomization(Scene): def construct(self): text1 = Text( 'Google', t2c={'[:1]': '#3174f0', '[1:2]': '#e53125', '[2:3]': '#fbb003', '[3:4]': '#3174f0', '[4:5]': '#269a43', '[5:]': '#e53125'}, font_size=58).scale(3) self.add(text1)
由于
Text
使用 Pango 渲染文本,因此渲染非英文字符非常容易示例: MultipleFonts ¶
from manim import * class MultipleFonts(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") japanese = Text( "日本へようこそ", t2c={"日本": BLUE} ) # works same as ``Text``. mess = Text("Multi-Language", weight=BOLD) russ = Text("Здравствуйте मस नम म ", font="sans-serif") hin = Text("नमस्ते", font="sans-serif") arb = Text( "صباح الخير \n تشرفت بمقابلتك", font="sans-serif" ) # don't mix RTL and LTR languages nothing shows up then ;-) chinese = Text("臂猿「黛比」帶著孩子", font="sans-serif") self.add(morning, japanese, mess, russ, hin, arb, chinese) for i,mobj in enumerate(self.mobjects): mobj.shift(DOWN*(i-3))
class MultipleFonts(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") japanese = Text( "日本へようこそ", t2c={"日本": BLUE} ) # works same as ``Text``. mess = Text("Multi-Language", weight=BOLD) russ = Text("Здравствуйте मस नम म ", font="sans-serif") hin = Text("नमस्ते", font="sans-serif") arb = Text( "صباح الخير \n تشرفت بمقابلتك", font="sans-serif" ) # don't mix RTL and LTR languages nothing shows up then ;-) chinese = Text("臂猿「黛比」帶著孩子", font="sans-serif") self.add(morning, japanese, mess, russ, hin, arb, chinese) for i,mobj in enumerate(self.mobjects): mobj.shift(DOWN*(i-3))
示例: PangoRender ¶
from manim import * class PangoRender(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") self.play(Write(morning)) self.wait(2)
class PangoRender(Scene): def construct(self): morning = Text("வணக்கம்", font="sans-serif") self.play(Write(morning)) self.wait(2)
测试
检查
Text
的创建是否正常工作>>> Text('The horse does not eat cucumber salad.') Text('The horse does not eat cucumber salad.')
方法
字体列表
初始化颜色。
属性
animate (动画)
用于动画化
self
的任何方法的应用。animation_overrides (动画覆盖)
颜色
depth (深度)
mobject 的深度。
fill_color (填充颜色)
如果存在多种颜色(用于渐变),则返回第一种颜色
字体大小
哈希种子
代表生成的mobject点结果的唯一哈希值。
height (高度)
mobject 的高度。
n_points_per_curve (每条曲线的点数)
sheen_factor (光泽因子)
stroke_color (描边颜色)
width (宽度)
mobject 的宽度。
- _original__init__(text, fill_opacity=1.0, stroke_width=0, color=None, font_size=48, line_spacing=-1, font='', slant='NORMAL', weight='NORMAL', t2c=None, t2f=None, t2g=None, t2s=None, t2w=None, gradient=None, tab_width=4, warn_missing_font=True, height=None, width=None, should_center=True, disable_ligatures=False, use_svg_cache=False, **kwargs)¶
初始化自身。有关准确签名,请参阅 help(type(self))。
- 参数:
text (str)
fill_opacity (float)
stroke_width (浮点数)
color (ParsableManimColor | None)
font_size (float)
line_spacing (float)
font (str)
slant (str)
weight (str)
t2c (dict[str, str])
t2f (dict[str, str])
t2g (dict[str, tuple])
t2s (dict[str, str])
t2w (dict[str, str])
gradient (tuple)
tab_width (int)
warn_missing_font (bool)
高度 (浮点数)
宽度 (浮点数)
should_center (bool)
disable_ligatures (bool)
use_svg_cache (bool)
- 返回类型:
无
- _set_color_by_t2c(t2c=None)[source]¶
为指定字符串设置颜色。
注意
已弃用 `Text._set_color_by_t2c` 方法自 v0.14.0 起已弃用,预计将在 v0.15.0 后移除。这是一个内部函数,您不应该使用它。
- _set_color_by_t2g(t2g=None)[source]¶
为指定字符串设置渐变颜色。其行为与
set_color_by_t2c
类似。注意
已弃用 `Text._set_color_by_t2g` 方法自 v0.14.0 起已弃用,预计将在 v0.15.0 后移除。这是一个内部函数,您不应该使用它。
- _text2hash(color)[source]¶
为文件名生成
sha256
哈希。- 参数:
color (ManimColor)
- _text2svg(color)[source]¶
使用 Pango 将文本转换为 SVG。
- 参数:
color (ManimColor)