Converts NSAttributedStrings into HTML (and back)
Ashton (AttributedStringHTMLTransformation) is an iOS and macOS library for fast conversion of NSAttributedStrings into HTML, and back. Ashton is battle-tested and used in MindNode, for persisting formatted strings.
The latest release is a complete rewrite in Swift focusing on improved performance and functional backwards compatibility to Ashton 1.x. The new codebase has a comprehensive test suite with a test coverage of > 90% and additional tests against the legacy 1.0 output.
Find out more about the launch of Ashton 2.0 in our Blog Post.
The following
NSAttributedString.Keyattributes are supported, when converting to
HTML: - [x] .backgroundColor (persisted as RGBA) - [x] .foregroundColor (persisted as RGBA) - [x] .underlineStyle (single, double, thick) - [x] .underlineColor (persisted as RGBA) - [x] .strikethroughColor (persisted as RGBA) - [x] .strikethroughStyle (single, double, thick) - [x] .font - [x] .paragraphStyle (text alignment) - [x] .baselineOffset - [x] NSSuperScript - [x] .link
As Ashton supports only tags which are necessary to persist the attributes mentioned above, not all HTML tags are supported when converting
HTML-->
AttributedString. Basically, Ashton converts an AttributedString into a concatenation of
span,
pand
atags with style attributes.
Supported HTML Tags: - [x] span - [x] p - [x] a - [x] em - [x] strong
The following style attribute keys are supported: - [x] background-color - [x] color - [x] text-decoration - [x] font - [x] text-align - [x] vertical-align - [x] Additional custom attributes (-cocoa-strikethrough-color, -cocoa-underline-color, -cocoa-baseline-offset, -cocoa-vertical-align, -cocoa-font-postscriptname, -cocoa-underline, -cocoa-strikethrough, -cocoa-fontFeatures)
Colors have to be formatted as rgba like
rgba(0, 0, 0, 1.000000).
Add this line to your Cartfile.
github "IdeasOnCanvas/Ashton"
The Swift Package Manager is a dependency manager integrated with the Swift build system. To learn how to use the Swift Package Manager for your project, please read the official documentation.
To add ZIP Foundation as a dependency, you have to add it to the
dependenciesof your
Package.swiftfile and refer to that dependency in your
target.
// swift-tools-version:5.1 import PackageDescription let package = Package( name: "", dependencies: [ .package(url: "https://github.com/IdeasOnCanvas/Ashton/", .upToNextMajor(from: "2.0.0")) ], targets: [ .target( name: "", dependencies: ["Ashton"]), ] )
After adding the dependency, you can fetch the library with:
$ swift package resolve
let htmlString = Ashton.encode(attributedString)
let attributedString = Ashton.decode(htmlString)
An example app can be found in the
/Exampledirectory. It can be used to test
NSAttributedString-> HTML ->
NSAttributedStringroundtrips and also to extract the HTML representation of an `NSAttributedString.
Ashton is brought to you by IdeasOnCanvas GmbH, the creator of MindNode for iOS, macOS & watchOS.