Plugin for handling layouts and sizing of layers automatically in Sketch based on the layer names. I call it Pixel Perfect 👾!
Download the zip-file and double click on the
.sketchplugin file to install.
Notice the naming of the layers to the left. Now select the artboard and execute
%L. See the result of the script below.
The script will go through all layers and sublayers to layout and size. It will also resize symbols to original size and even go inside the symbols themselves to make them pixel perfect too.
There are three ways to execute the script:
⌘Lwith selection: only the selected layers and it’s sublayers will be fixed.
⌘Lwithout selection: all layers and sublayers on the current page will be fixed.
^⌘L: all layers and sublayers in the entire Sketch file will be fixed.
By naming your layers with the formatting described below, your layers will take on certain properties depending on that name. These properties can be separated by
: and can be contained, in the name, by putting them in brackets.
Set the layer width in pixels with
w as the property index.
Addition or Subtraction
Add or subtract pixels to the width of the layer. This property works best together with a static width property or to add or subtract width to a symbol instance.
Set width relative the layer group width. If the layer group is also specified with a width percentage this will take into consider it’s layer group width. If you specify two
% the width will be set to the master or the artboard containing the layer independent of how many layer groups it’s in.
Min or Max
Defined a minimum or maximum width in pixels for a layer group. If the sublayers within the group are less or greater then the width it will resize the group to the specified value, if not it won’t affect the layer.
width. Works exactly the same but with
h as the property index.
Padding can either be defined by solo numbers, inspired by css, and will be regarded in the order of
left, or in a specific direction by adding the property indexes;
pt for top,
pr for right,
pb for bottom, and
pl for left.
Padding works either next to a layer named
bg (case-insensitive), which will be the layer affected, or with a layer named
bg inside the group. Padding applied to an artboard or symbol will affect it’s size given the content.
Margin properties places the layer certain pixels off the sides relative it’s layer group. The property indexes are;
t for top,
r for right,
b for bottom, and
l for left. If no number is specified it will be regarded as 0.
Stacks layers horizontally within a layer group. The order in the layer is the order they will be stacked. The property indexes are;
xt for top aligned,
x for center and
xb for bottom aligned. The number following the index will account for the distance between the stacked objects.
stack-horizontally. Works exactly the same but with
yl for left aligned,
y for center and
yr for right aligned as the property index.
Centers the layer horizontally in it’s layer group. Add a positive or negative number to offset it from it’s center in pixels.
h is the property index and can be used on it’s own but if you want to apply offset you need to use
c instead so to not confuse it with addition and subtraction of height.
center-horizontally. Works exactly the same but with
v as the property index.
Things to note
Symbol masters and Artboards
Symbol masters will automatically resize itself to fit the content within if nothing else is specified, artboards will not. Both can have properties similar to layers.
Will be set to
Auto if there are no size properties apply to it, otherwise it will be
Fixed after resizing it to fit it’s content.
If you don’t want the script to apply to a certain layer, add
[ignore] to the name.
Depending on the properties you have given a layer, constraints will be set as see fit. In short, margins and paddings will affect
left constraints while width and height will affect those constraints.
There are some shorthands for
margin, for instance;
tl places the object to the top left,
tr to the top right,
br to the bottom right, and
bl to the bottom left. For padding their are;
p for adding padding to all sides,
ptb for bottom and top, and
prl for left and right. All these property indices can be followed by a number and are permutable.