<p align="center" >
<img src="https://raw.githubusercontent.com/malcommac/SwiftRichString/master/SRRLogo.png" width=450px alt="SwiftRichString" title="SwiftRichString">
</p>
[](http://cocoadocs.org/docsets/SwiftRichString) [](http://cocoadocs.org/docsets/SwiftRichString)
[](https://github.com/Carthage/Carthage)
[](http://twitter.com/danielemargutti)
SwiftRichString is a lightweight library which allows to create and manipulate attributed strings easily both in iOS, macOS, tvOS and even watchOS.
It provides convenient way to store styles you can reuse in your app's UI elements, allows complex tag-based strings rendering and also includes integration with Interface Builder.
It even support **iOS 11's Dynamic Type**!
If you manipulate `NSAttributedString` in Swift, SwiftRichString allows you to keep your code manteniable, readable and easy to evolve.
## Features Highlights
Want to know what SwiftRichString can do in your app? Lets take a look to these feature highlights!
### 1. Easy Styling
The main concept behind this lib is the `Style`: a style is just a collection of text attributes you can apply to a string. The following example show how to create a style an produce an attributed string with it:
```swift
let style = Style {
$0.font = SystemFonts.AmericanTypewriter.font(size: 25) // just pass a string, one of the SystemFonts or an UIFont
$0.color = "#0433FF" // you can use UIColor or HEX string!
$0.underline = (.patternDot, UIColor.red)
$0.alignment = .center
}
let attributedText = "Hello World!".set(style: style) // et voilà!
```
### 2. Global Styles & Interface Builder Integration
Styles can be also registered globally and reused in your app.
Just define your own style and register using `Styles.register()` function:
```swift
let myStyle = Style { // define style's attributes... }
Styles.register("MyStyle", style: style)
```
Now you can reuse it everything in your app; SwiftRichString exposes a `styleName` property for the most common text containers and you can set it directly in Interface Builder:
### 3. Complex Rendering with tag-based strings
SwiftRichString allows you to render complex strings by parsing text's tags: each style will be identified by an unique name (used inside the tag) and you can create a `StyleGroup` which allows you to encapsulate all of them and reuse as you need (clearly you can register it globally).
```swift
// Create your own styles
let normal = Style {
$0.font = SystemFonts.Helvetica_Light.font(size: 15)
}
let bold = Style {
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
$0.color = UIColor.red
$0.backColor = UIColor.yellow
}
let italic = normal.byAdding {
$0.traitVariants = .italic
}
// Create a group which contains your style, each identified by a tag.
let myGroup = StyleGroup(base: normal, ["bold": bold, "italic": italic])
// Use tags in your plain string
let str = "Hello <bold>Daniele!</bold>. You're ready to <italic>play with us!</italic>"
self.label?.attributedText = str.set(style: myGroup)
```
That's the result!
--
## Documentation
**Are you using SwiftRichString 1.x in your project? Don't miss to take a look at [Migration section of the documentation](#migration).**
You can still use the 1.x release by using tagged version 1.1.0.
**Table Of Contents**
Full changelog is available in [CHANGELOG.md](CHANGELOG.md) file.
- [Versions (1.x, 2.x and old Swift 3.x branch)](#versions)
- [Introduction to `Style`, `StyleGroup` & `StyleRegEx`](#stylestylegroup)
- [Introduction](#introduction)
- [String & Attributed String concatenation](#concatenation)
- [Apply styles to `String` & `Attributed String`](#manualstyling)
- [Fonts & Colors in `Style`](#fontscolors)
- [Derivating a `Style`](#derivatingstyle)
- [Support iOS's `Dynamic Type`](#dynamictype)
- [Dynamic Attributes from Tag's Params](#dynamicattributes)
- [The `StyleManager`](#stylemanager)
- [Register globally available styles](#globalregister)
- [Defer style creation on demand](#defer)
- [Assign style using Interface Builder](#ib)
- [All properties of `Style`](#props)
Other info:
- [Migration from SwiftRichString 1.x](#migration)
- [Requirements](#requirements)
- [Installation](#installation)
- [Contributing](#contributing)
- [Copyright](#copyright)
<a name="versions"/ rel='nofollow' onclick='return false;'>
### Versions
- **SwiftRichString 2.x branch (current)**. The latest version is [2.0.2](https://github.com/malcommac/SwiftRichString/releases/tag/2.0.2).
- **SwiftRichString 1.x branch (supported)**. Use [1.1.0 tag](https://github.com/malcommac/SwiftRichString/releases/tag/1.1.0). Its compatible with Swift 4.x.
- **Swift 3.x (no longer mantained)**. Use [0.9.1 release](https://github.com/malcommac/SwiftRichString/releases/tag/0.9.10).
<a name="stylestylegroup"/ rel='nofollow' onclick='return false;'>
## Introduction to `Style`, `StyleGroup`, `StyleRegEx`
<a name="introduction"/ rel='nofollow' onclick='return false;'>
### Introduction
The main concept behind SwiftRichString is the use of `StyleProtocol` as generic container of the attributes you can apply to both `String` and `NSMutableAttributedString`.
Concrete classes derivated by `StyleProtocol` are: `Style`, `StyleGroup` and `StyleRegEx`.
Each of these classes can be used as source for styles you can apply to a string, substring or attributed string.
#### `Style`: apply style to strings or attributed strings
A `Style` is a class which encapsulate all the attributes you can apply to a string. The vast majority of the attributes of both AppKit/UIKit are currently available via type-safe properties by this class.
Creating a `Style` instance is pretty simple; using a builder pattern approach the init class require a callback where the self instance is passed and allows you to configure your properties by keeping the code clean and readable:
```swift
let style = Style {
$0.font = SystemFonts.Helvetica_Bold.font(size: 20)
$0.color = UIColor.green
// ... set any other attribute
}
let attrString = "Some text".set(style: style) // attributed string
```
#### `StyleGroup`: Apply styles for tag-based complex string
`Style` instances are anonymous; if you want to use a style instance to render a tag-based plain string you need to include it inside a `StyleGroup`. You can consider a `StyleGroup` as a container of `Styles` (but, in fact, thanks to the conformance to a common `StyleProtocol`'s protocol your group may contains other sub-groups too).
```swift
let bodyStyle: Style = ...
let h1Style: Style = ...
let h2Style: Style = ...
let group = StyleGroup(base: bodyStyle, ["h1": h1Style, "h2": h2Style])
let attrString = "Some <h1>text</h1>, <h2>welcome here</h2>".set(style: group)
```
The following code defines a group where:
- we have defined a base style. Base style is the style applied to the entire string and can be used to provide a base ground of styles you want to apply to the string.
- we have defined two other styles named `h1` and `h2`; these styles are applied to the source string when parser encounter some text enclosed by these tags.
#### `StyleRegEx`: Apply styles via regular expressions
`StyleRegEx` allows you to define a style which is applied when certain regular expression is matched inside the target string/attributed string.
```swift
let emailPattern = "([A-Za-z0-9_\\-\\.\\+])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]+)"
let style = StyleRegEx(pattern: emailPattern) {
$0.color = UIColor.red
$0.backColor = UIColor.yellow
}
let attrString = "My email is hello@danielemargutti.com and my website is http://www.danielemargutti.com".(style: style!)
```
The re