├── Align Text Layers.sketchplugin ├── CHANGELOG.md ├── LICENSE.md ├── README.md └── helpers.js /Align Text Layers.sketchplugin: -------------------------------------------------------------------------------- 1 | // (cmd option l) 2 | #import 'helpers.js' 3 | 4 | format(); -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | Changelog 3 | ========= 4 | 5 | 6 | # 1.x release 7 | 8 | ## v1.0.0 (master) 9 | 10 | - Major: initial release 11 | 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 David Frank 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | sketch-text-align 3 | ================= 4 | 5 | Properly align text layers regardless of their rectangle box in Sketch 6 | 7 | ![demo][demo-image] 8 | 9 | 10 | # Motivation 11 | 12 | Because Sketch v3.x still has less than ideal support for font-size and line-height, especially when you are dealing with CJK language (Some say it's a limit of OSX). This plugin allow you to align text as imagined, ignoring those incorrectly calculated text rectangles. 13 | 14 | This plugin has been tested on Sketch v3.2 and v3.3 beta 15 | 16 | 17 | # Install 18 | 19 | 1. Download ZIP of this repo. 20 | 2. Extract and rename folder to suit your taste. 21 | 3. Put the folder in your Sketch plugin folder (Use `Plugins>Reveal Plugins Folder` to find it). 22 | 23 | Folder name and plugin filename will show up as items in Sketch `Plugins` menu. 24 | 25 | 26 | # Usage 27 | 28 | 1. Select multiple text layers. 29 | 2. `Alt + Cmd + L` or use `Plugins` menu. 30 | 3. Follow the dialog prompt. 31 | 32 | 33 | # Note 34 | 35 | This plugin does not fix multiline text layer or weird padding issues, it makes aligning multiple text layers much easier instead (so you can avoid wordwrap in Sketch). 36 | 37 | 38 | # License 39 | 40 | MIT 41 | 42 | [demo-image]: http://i.imgur.com/Bx1hpLA.png 43 | -------------------------------------------------------------------------------- /helpers.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * helpers.js 4 | * 5 | * A helper for aligning text layers 6 | */ 7 | 8 | function format() { 9 | var app = NSApplication.sharedApplication(); 10 | 11 | if (selection.count() < 2) { 12 | app.displayDialog('Please select at least 2 text layers'); 13 | return; 14 | } 15 | 16 | var scale = parseFloat(doc.askForUserInput_initialValue('Input your expected line-height (unit em)', '1.5')); 17 | if (!scale) { 18 | return; 19 | } 20 | 21 | var prevLayer, prevFrame; 22 | 23 | for (var i = selection.count() - 1; i >= 0; i--) { 24 | var layer = selection[i]; 25 | 26 | if (layer.className() != 'MSTextLayer') { 27 | continue; 28 | } 29 | 30 | if (!prevLayer) { 31 | prevLayer = layer; 32 | continue; 33 | } 34 | 35 | var pos = position(prevLayer, scale); 36 | align(layer, pos); 37 | 38 | prevLayer = layer; 39 | }; 40 | }; 41 | 42 | function position(layer, scale) { 43 | var frame = layer.frame(); 44 | var pos = {}; 45 | pos.x = frame.x(); 46 | pos.y = frame.y() + layer.fontSize() * scale; 47 | 48 | return pos; 49 | }; 50 | 51 | function align(layer, pos) { 52 | var frame = layer.frame(); 53 | frame.setX(pos.x); 54 | frame.setY(pos.y); 55 | }; 56 | --------------------------------------------------------------------------------