文本对象¶
用于显示(非LaTeX)文本的Mobjects。
注意
正如你可以使用Tex和MathTex(来自模块tex_mobject)将LaTeX插入到你的视频中一样,你可以使用文本来添加普通文本。
重要
请参阅相应的教程没有LaTeX的文本,特别是关于字体的信息。
The simplest way to add text to your animations is to use the 文本 class. It uses the Pango library to render text.
With Pango, you are also able to render non-English alphabets like 你好 or こんにちは or 안녕하세요 or مرحبا بالعالم.
示例
示例:HelloWorld ¶
from manim import *
class HelloWorld(Scene):
def construct(self):
text = Text('Hello world').scale(3)
self.add(text)
class HelloWorld(Scene):
def construct(self):
text = Text('Hello world').scale(3)
self.add(text)
示例:文本对齐 ¶
from manim import *
class TextAlignment(Scene):
def construct(self):
title = Text("K-means clustering and Logistic Regression", color=WHITE)
title.scale(0.75)
self.add(title.to_edge(UP))
t1 = Text("1. Measuring").set_color(WHITE)
t2 = Text("2. Clustering").set_color(WHITE)
t3 = Text("3. Regression").set_color(WHITE)
t4 = Text("4. Prediction").set_color(WHITE)
x = VGroup(t1, t2, t3, t4).arrange(direction=DOWN, aligned_edge=LEFT).scale(0.7).next_to(ORIGIN,DR)
x.set_opacity(0.5)
x.submobjects[1].set_opacity(1)
self.add(x)
class TextAlignment(Scene):
def construct(self):
title = Text("K-means clustering and Logistic Regression", color=WHITE)
title.scale(0.75)
self.add(title.to_edge(UP))
t1 = Text("1. Measuring").set_color(WHITE)
t2 = Text("2. Clustering").set_color(WHITE)
t3 = Text("3. Regression").set_color(WHITE)
t4 = Text("4. Prediction").set_color(WHITE)
x = VGroup(t1, t2, t3, t4).arrange(direction=DOWN, aligned_edge=LEFT).scale(0.7).next_to(ORIGIN,DR)
x.set_opacity(0.5)
x.submobjects[1].set_opacity(1)
self.add(x)
类
函数
- register_font(font_file)[source]¶
暂时将一个字体文件添加到Pango的搜索路径中。
这会在多个地方搜索字体文件。搜索的顺序如下所述。
绝对路径。
在
assets/fonts文件夹中。在
font/文件夹中。在同一目录下。
- Parameters:
font_file (str | Path) – 要添加的字体文件。
示例
使用
with register_font(...)将字体文件添加到搜索路径。with register_font("path/to/font_file.ttf"): a = Text("Hello", font="Custom Font Name")
- Raises:
FileNotFoundError: – 如果字体不存在。
AttributeError: – 如果在macOS上使用此方法。
.. 重要 :: – 此方法适用于macOS上的
ManimPango>=v0.2.3。在macOS上使用此方法时,如果版本低于此版本,将会引发AttributeError。
- Parameters:
font_file (str | Path)
- remove_invisible_chars(mobject)[来源]¶
用于从某些mobjects中移除不需要的不可见字符的函数。
- Parameters:
mobject (SVGMobject) – 任何我们想要从中移除不需要的不可见字符的SVGMobject。
- Returns:
没有不需要的不可见字符的SVGMobject。
- Return type: