5

I was using Sketch to make a cover photo, and there are many words in it. I have to convert every text to online, so I have to open layer folders and select each text in it by myself.

Is there any faster way to convert all texts in one single step?

3 Answers 3

4

Here is my approach. I found sketch plugin which converts text layer to outlines, but it converts only selected text layer. I modified it so it converts all text layers on current page. Here is instructions, for instance we have photo with several text layers on in: enter image description here

Go to menu select Plugins > Custom Plugin... Text area will appear, copy paste this code:

(function(){
    function convertToOutlines(layer) {
        if(!layer.isKindOfClass(MSTextLayer)) return;

        var path=layer.bezierPathWithTransforms(); // Get text layers' outline as NSBezierPath.

        var parent=layer.parentGroup();
        var shape=MSShapeGroup.shapeWithBezierPath(path); // Create a vector shape with the outline.

        // Set the style previously used in text layer.
        shape.style = layer.style();

        // If text layer doesn't have a fill style, we create it with the color used for the text.
        var style=shape.style();
        if(!style.fill()) {
            var fill=style.fills().addNewStylePart();
            fill.color = MSColor.colorWithNSColor(layer.style().textStyle().attributes().NSColor);
        }

        // Copy name and selection status.
        var isSelected=layer.isSelected();
        shape.name = layer.name();
        shape.setIsSelected(isSelected);

        // Remove text layer.
        parent.removeLayer(layer);

        // Add a newly created shape to the text layers' parent group.
        parent.addLayers([shape]);

        return shape;
    }

    var doc = context.document;
    var page = [doc currentPage];
    var layers = page.children();
    for (var i = 0; i < layers.count(); i++) {
        var layer = layers.objectAtIndex(i);
        if(layer) {
            var vectorizedTextLayer=convertToOutlines(layer);
            print(vectorizedTextLayer);
        }
    }
})();

It should look like this, there you can save it and run it: enter image description here

After running the script all text layers becomes outlined: enter image description here

Github repository of the plugin: https://github.com/zholdas/Texts-to-Outlines

Initial sketch plugin, my is based on https://github.com/turbobabr/Sketch-Plugins-Cookbook/blob/master/Samples/Convert%20Text%20Layer%20to%20Outlines.sketchplugin

1
  • 1
    Nice plugin, thanks man. It does work and help! :D I hope I could apply it to specific artboard in the future.
    – p3nchan
    Sep 9, 2015 at 7:04
0

Above answer is not working for sketch 3.8 and above. It gives fills().addNewStylePart is undefined error on line 17 while running it as custom script. While searching on the internet i found that things have been changed for addNewStylePart. In the past, if you wanted to add a new fill, border or shadow to a layer you'd do:

layer.style().fills().addNewStylePart()

Starting with Sketch 3.8, the way to do that now is:

layer.style().addStylePartOfType(0) // To add a new fill

For more details see this source

Final working script looks like:

(function(){
    function convertToOutlines(layer) {
        if(!layer.isKindOfClass(MSTextLayer)) return;

        var path=layer.bezierPathWithTransforms(); // Get text layers' outline as NSBezierPath.

        var parent=layer.parentGroup();
        var shape=MSShapeGroup.shapeWithBezierPath(path); // Create a vector shape with the outline.

        // Set the style previously used in text layer.
        shape.style = layer.style();

        // If text layer doesn't have a fill style, we create it with the color used for the text.
        var style=shape.style();
        if(!style.fill()) {
            var fill=style.addStylePartOfType(0);
            fill.color = MSColor.colorWithNSColor(layer.style().textStyle().attributes().NSColor);
        }

        // Copy name and selection status.
        var isSelected=layer.isSelected();
        shape.name = layer.name();
        shape.setIsSelected(isSelected);

        // Remove text layer.
        parent.removeLayer(layer);

        // Add a newly created shape to the text layers' parent group.
        parent.addLayers([shape]);

        return shape;
    }

    var doc = context.document;
    var page = [doc currentPage];
    var layers = page.children();
    for (var i = 0; i < layers.count(); i++) {
        var layer = layers.objectAtIndex(i);
        if(layer) {
            var vectorizedTextLayer=convertToOutlines(layer);
            print(vectorizedTextLayer);
        }
    }
})();
1
  • I use Sketch 52.5 and nothing happens when I run the plugin. Any idea?
    – Dollique
    Dec 12, 2018 at 9:37
0

Found a way to do this that is

  • super quick and easy
  • doesn't require plugins or advanced plugin knowledge
  • actually generates a cleaner SVG export

Here's what you do:

  1. Open your Sketch file and highlight everything you want to export
  2. cmd + c (copy)
  3. Open Adobe Illustrator, new file
  4. cmd + v (paste)
  5. Select All text, Type > Create Outlines
  6. Switch back to Sketch
  7. Sketch > Quit Sketch
1
  • A downside to this is of course that you need to have Illustrator. I'm not a Sketch user, but it seems that following your recipe will leave you with Illustrator open with the outlined text in an unsaved document and Sketch closed? Perhaps you should edit and add how to complete the task.
    – Wolff
    Aug 18, 2020 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.