文本¶
限定名称: 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)[来源]¶
基础类:
SVGMobject
显示使用Pango渲染的(非LaTeX)文本。
文本对象的行为类似于
VGroup
,可以迭代给定文本中的所有字符。特别是,可以进行切片操作。- Parameters:
文本 (str) – 需要创建为mobject的文本。
font (str) – 用于渲染文本的字体家族。这可以是系统字体,也可以是使用register_font()加载的字体。请注意,字体家族名称可能因操作系统而异。
warn_missing_font (bool) – 如果为True(默认值),当字体不存在于manimpango.list_fonts()返回的(区分大小写的)字体列表中时,Manim将发出警告。
fill_opacity (float)
stroke_width (float)
颜色 (ParsableManimColor | 无)
font_size (float)
line_spacing (float)
slant (str)
weight (str)
t2c (字典[字符串, 字符串])
t2f (字典[字符串, 字符串])
t2g (dict[str, tuple])
t2s (字典[字符串, 字符串])
t2w (字典[字符串, 字符串])
梯度 (元组)
tab_width (int)
高度 (浮点数)
width (float)
should_center (bool)
disable_ligatures (bool)
use_svg_cache (bool)
- Returns:
类似mobject的
VGroup
。- Return type:
示例
示例: 示例1文本 ¶
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)
由于
文本
使用Pango来渲染文本,因此可以轻松渲染非英文字符:示例:多种字体 ¶
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('The horse does not eat cucumber salad.') Text('The horse does not eat cucumber salad.')
方法
font_list
初始化颜色。
属性
animate
用于动画化
self
的任何方法的应用。animation_overrides
color
depth
mobject的深度。
fill_color
如果有多种颜色(用于渐变),则返回第一个颜色
font_size
hash_seed
表示生成的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))。
- Parameters:
文本 (字符串)
fill_opacity (float)
stroke_width (float)
颜色 (ParsableManimColor | 无)
font_size (float)
line_spacing (float)
字体 (字符串)
slant (str)
weight (str)
t2c (字典[字符串, 字符串])
t2f (字典[字符串, 字符串])
t2g (dict[str, tuple])
t2s (字典[字符串, 字符串])
t2w (字典[字符串, 字符串])
梯度 (元组)
tab_width (int)
warn_missing_font (bool)
高度 (浮点数)
width (float)
should_center (bool)
disable_ligatures (bool)
use_svg_cache (bool)
- Return type:
无
- _set_color_by_t2c(t2c=None)[来源]¶
为指定的字符串设置颜色。
注意
已弃用 自 v0.14.0 起,方法 Text._set_color_by_t2c 已被弃用,并预计在 v0.15.0 之后移除。这是一个内部函数,您无论如何都不应该使用它。
- _set_color_by_t2g(t2g=None)[source]¶
- Sets gradient colors for specified
字符串。行为类似于
set_color_by_t2c
。
注意
已弃用 自 v0.14.0 起,方法 Text._set_color_by_t2g 已被弃用,并预计在 v0.15.0 之后移除。这是一个内部函数,无论如何你都不应该使用它。
- _text2hash(color)[source]¶
为文件名生成
sha256
哈希值。- Parameters:
颜色 (ManimColor)
- _text2svg(color)[source]¶
使用Pango将文本转换为SVG。
- Parameters:
颜色 (ManimColor)