Coder Social home page Coder Social logo

malczak / textpaths Goto Github PK

View Code? Open in Web Editor NEW
36.0 6.0 4.0 567 KB

Split NSAttributedText into glyph CGPath, convert NSAttributedText to SVG

License: MIT License

Swift 97.08% Ruby 2.92%
coretext ios swift cgpath text-to-vectors uikit ios-swift nsattributedstring svg vector-graphics

textpaths's Introduction

TextPaths

TextPaths is an utility for converting NSAttributedText to vector representation where each character of an input text is represented as a CGPAth glyph. TextPaths also returns typographic propeties for lines and entire text flow. For in-depth knowledge on how iOS handles text see Using Text Kit to Draw and Manage Text chapter in documentation.

Source NSAttributedText is converted into a tree like representation with char glyphs on leafs.

 TextPath
    ⎿ TextPathFrame[]
        ⎿ TextPathLine[]
            ⎿ TextPathGlyph[]

    ⎿ ComposedTextPath

TextPathFrame - text frame representation

TextPathLine - single text line representation

There is also a text bounds rectangle available. Text bounds rectangle is equal or less then line bounds rectangle. On image below this metric is show in purple.

TextPathGlyph - single character representation (glyph)

ComposedTextPath - composed path of entire input text. This is illustrated on all images above in black.

All illustrations were created with TextDecompose sample application.

Important notes

  • UITextView has extra edge insets (padding) of apporx 8px
  • UIKit treats text different than CoreText when different font size is used to trailing line-break \n character. TextPaths instead of following CoreText is using the same approach as UIKit. See code comments in TextPathLine class for effectiveDescent property.

Production use

This repo is used in my apps

Sample Applications

A sample showing how NSAttributedString is decomposed into CGPaths keeping all attributes and typographic properties. See screenshots below

NSAttributedString to SVG conversion. Entire NSAttributedString is converted in to one merged CGPath and then serialized as SVG path. Resulting SVG is presented in WKWebView for comparison.

Falling letters counter application shows how to to use TextPaths for text animations. In this example TextPaths is used to animated UILabel value change.

Another animation example showing how to animate multiline NSAttributedText.

Screenshots

![asas](https://raw.githubusercontent.com/malczak/TextPaths/master/_Assets/textpaths-nsattributedtext-decompose.jpg | height=480)

textpaths's People

Contributors

malczak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

textpaths's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.