textLayer

TextLayer可以帮助你在 Framer 中更加方便地进行文字排版。你可以使用一个字符串来定义文字内容,并使用它的一些特有属性来渲染它的样式。几乎所有的文字图层属性都能在 CSS 中找到,你可以改变字体、改变样式、调整行高、添加内边距或阴影等等。

A TextLayer allows you to work with type in Framer. Define text within a string, and use its unique properties to style it. Almost all property names match the CSS text styles. You can change the typeface, change the style, adjust the line height, add padding, shadows and much more.

text.text <string>

文本图层的文字内容,默认情况下它的颜色是 #888

The text content. By default, the color is set to #888.

# 创建一个文字图层
title = new TextLayer
text: "Title"

text.template <object>

设置模板插值(模板内的被标记变量替换值)的值,模板插值是指使用 {} 包裹的变量。

Set values for template tags in text. Template tags are wrapped in {}.

# 创建一个带模板插值的文本图层
layerA = new TextLayer
text: "{speed}KM/h"
# 设置模板插值 speed 为 50
layerA.template =
speed: 50
# 你可以像这样简写,它会替换掉第一个标记变量
layerA.template = 50
# 这两种写法文本图层的结果都是 "50KM/h".

text.templateFormatter <object>

格式化模板插值。

Format template values to inject into template tags.

# 创建带模板插值的文本图层
layerA = new TextLayer
text: "{speed}{unit}/h"
# 格式化模板插值,使其只保留两位小数
layerA.templateFormatter =
speed: (value) ->
Utils.round(value, 2)
# 使用动画形式让它的值从 0 变化到 100
layerA.animate
template:
speed: 100
unit: "KM"

使用这种简写方式来格式化第一个模板插值。

Here’s a shorthand way to format the first template tag only.

# 格式化第一个模板插值,使其只保留两位小数
layerA.templateFormatter = (value) ->
Utils.round(value, 2)

text.textReplace(from, to)

查找文本图层中的某一段并替换。

Finds and replaces a piece of text.

# 将 0:00 替换为 45:00
layerA = new TextLayer
text: "Total time: 0:00"
layerA.textReplace("0:00", "45:00")

text.fontSize <number>

设置文本图层的字号,默认是 40 像素。

The size of the text. By default, it's set to 40.

# 设置字号为64px
title = new TextLayer
fontSize: 64

text.fontFamily <string>

设置当前字体。默认情况下,它会使用你预览设备的系统字体。

The current typeface of the text. By default, it's set to use the system typeface of the device you're currently previewing on.

  • Apple设备 : SF UI.

  • Google设备 : Roboto.

  • Microsoft设备 : Segoe UI.

# 创建一个文字图层
title = new TextLayer
fontFamily: "-apple-system"

text.fontWeight <number>

字体的粗细,默认是 400 。

The weight of the text. By default, it's set to 400.

# 创建一个文字图层
title = new TextLayer
fontWeight: 400

text.fontStyle <string>

设置字体的风格。

The style of the text.

# 设置字体为斜体风格
title = new TextLayer
fontStyle: "italic"

你可以选择italicboldoblique

You can choose between italic, bold and oblique.

  • italic 会显示一个斜体的字体样式。

  • bold 会显示一个加粗的字体样式。

  • oblique 会显示一个倾斜的字体样式。

# 设置字体为加粗风格
title = new TextLayer
fontStyle: "bold"

text.font <string>

在一行内定义字体的所有属性。你可以站照这样的顺序写:"fontStyle fontWeight fontSize/lineHeight fontFamily"。

A shorthand to define all font styles using a single line. These properties can be set (in order): "fontStyle fontWeight fontSize/lineHeight fontFamily".

# 在一行内设置字体样式属性
title = new TextLayer
font: "300 64px/1 -apple-system, Helvetica Neue"

需要注意的是fontSizefontFamily是必须要写的。

Note that the fontSize and fontFamily properties are required.

# 设置字号和字体
title = new TextLayer
font: "64px -apple-system"

text.padding <object>

文字图层内边距,默认是 0 。

The padding of the text. By default, it's set to 0.

# 设置内边距
title = new TextLayer
padding: 40

你也可以给每个方向独立设置不同的内边距。

You can also define padding of each side individually.

# 给每个方向设置内边距
title = new TextLayer
padding:
top: 40
left: 20
bottom: 40
right: 20

当然你也可以使用简写方式:用horizontal设置左右内边距,用vertical设置上下内边距。

Left and right padding can be defined by using the horizontal shorthand. Top and bottom padding can also be defined using the vertical shorthand.

# 设置左右和上下内边距
title = new TextLayer
padding:
horizontal: 40
vertical: 80

text.lineHeight <number>

文字图层的行高,默认情况下是 1.25 倍。也就是说,当你设置字号为 40px 时,行高就是 50px 。

The line height of the text. By default, it's set to 1.25. This means that if you've set the fontSize to 40, the line-height will effectively be 50px.

# 设置行高
title = new TextLayer
lineHeight: 1.5

text.letterSpacing <number>

设置字间距,默认是 0 。

The letter spacing of the text in pixels. Set to 0 by default.

# 设置字间距
title = new TextLayer
letterSpacing: 2&gt;

text.wordSpacing <number>

设置词间距,默认是 0 。

The letter spacing of the words in pixels. Set to 0 by default.

# 设置词间距
title = new TextLayer
wordSpacing: 10

text.textAlign <string>

设置文字对齐方式。

The alignment of the text.

# 居中对齐文字
title = new TextLayer
textAlign: "center"

你可以选择的对齐方式有leftrightcenter

You can choose between left, right and center.

# 多种对齐方式
title.textAlign = "left"
title.textAlign = "right"
title.textAlign = "center"

当然,你也可以使用Align对象。

Alternatively, you can also use the Align class.

# 居中对齐文字
title = new TextLayer
textAlign: Align.center

text.textTransform <string>

设置字母大小写。

The capitalization of the text.

# 大写所有字母
title = new TextLayer
textTransform: "uppercase"

你可以设置为uppercaselowercasecapitalize

You can choose between uppercase, lowercase and capitalize.

# 多种字母大小写形式
title.textTransform = "uppercase"
title.textTransform = "lowercase"
title.textTransform = "capitalize"

text.textDecoration <string>

设置文字的修饰。

The decoration of the text.

# 给文字添加下划线
title = new TextLayer
textDecoration: "underline"

你可设置为underlineoverlineline-through

You can choose between underline, overline and line-through.

# 给文字添加上划线
title = new TextLayer
textDecoration: "overline"

text.textIndent <number>

设置首字缩进。

The indentation of the first paragraph of text in pixels.

# 首字缩进20像素
title = new TextLayer
textIndent: 20

text.textOverflow <string>

给溢出的文字添加省略号。

Add an ellipsis to overflowing text content.

# 单行截取
paragraph = new TextLayer
textOverflow: "ellipsis"
text:
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
"""

如果你设置了文字图层的高度,就可以给多行文字溢出的部分添加省略号。

If you set the height, you’ll be able to add an ellipsis to multiple lines.

# 多行截取
paragraph = new TextLayer
textOverflow: "ellipsis"
height: 100
text:
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
"""

text.direction <string>

文字方向,默认是从左向右。

The direction of the text. The direction of the text. Set to left-to-right by default.

# 设置文字方向
title = new TextLayer
direction: "right-to-left"

你可以设置为right-to-left (rtl)left-to-right (ltr)

You can choose between right-to-left (rtl) and left-to-right (ltr).

# 设置文字方向
title = new TextLayer
direction: "rtl"

text.truncate <boolean>

设置文字溢出显示省略号的简写。

A shorthand of setting textOverflow to "ellipsis".

# 单行截取
paragraph = new TextLayer
truncate: true
text:
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
"""

如果你设置了文字图层的高度,就可以给多行文字溢出的部分添加省略号。

If you set the height, you’ll be able to add an ellipsis to multiple lines.

#多行截取
paragraph = new TextLayer
truncate: true
height: 100
text:
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
"""

text.shadowX <number>

设置文字阴影在 x 轴方向的偏移,正值将会产生一个从右边缘伸出来的阴影,负值将会产生一个从左边缘伸出的阴影。

Defines the shadow direction on the x-axis. A positive value will produce a shadow from the right edge of a layer, whereas a negative value will produce a shadow from the left edge.

# 设置横向文字阴影。
title = new TextLayer
shadowX: 10

text.shadowY <number>

设置文字阴影在 y 轴方向的偏移,正值将会产生一个从下边缘伸出来的阴影,负值将会产生一个从上边缘伸出的阴影。

Defines the shadow direction on the y-axis. A positive value will produce a shadow from the bottom edge of a layer, whereas a negative value will produce a shadow from the top edge.

# 设置纵向文字阴影
title = new TextLayer
shadowY: 10

text.shadowBlur <number>

给文字阴影设置高斯模糊,它的只是一个数字,默认为 0 。

Adds a gaussian blur to the shadowX or shadowY property. shadowBlur is defined with a number. The default value is 0.

# 设置文字阴影
title = new TextLayer
shadowY: 1
shadowBlur: 4

text.shadowColor <string>

给文字阴影设置颜色,使用 CSS 颜色格式。

Sets the color of a layers shadow. The color is expressed as a string in the CSS color format.

# 设置文字阴影颜色
title = new TextLayer
shadowY: 10
shadowColor: "rgba(0,0,0,0.2)"