Sketch API - 文本样式

时间:2017-10-06 07:33:10

标签: api text sketch-3

我在使用Sketch API / Sketch API文档时遇到了很多麻烦。 在文本(font-size,font-family等)上添加样式的正确方法是什么?

这是我在循环中的文字。

 var text = group.newText(
        {
            text: Colors.groupNames[index],
            frame: new api.Rectangle(50, 0, 50, 50),
        }
    );

1 个答案:

答案 0 :(得分:0)

你应该可以这样做:

var text = group.newText({font: NSFont.fontWithName_size("OpenSans-Bold", 15), text:"Hello World"})

但是API中存在一个错误,解决方法是:

var text = group.newText({text: "Hello World"})
text._object.setFont(NSFont.fontWithName_size("OpenSans-Bold", 15))

字体名称是不带扩展名的字体文件名。你可以在〜/ Library / Fonts中找到它们。

您可以在文本图层上设置的其他属性列于here,以下是docs中的示例:

var text = group.newText({fixedWidth: true, alignment: NSTextAlignmentCenter, systemFontSize: 24, text:"Hello World"})
text.frame = new sketch.Rectangle(0, 160, 200, 30) // adjust the frame last, after the font/size/alignment etc has been set up