Skip to content

Commit 4233c69

Browse files
committed
Update README.md.
1 parent 42918fc commit 4233c69

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

README.md

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
[![Build Status](https://travis-ci.org/natestedman/Attributed.svg?branch=master)](https://travis-ci.org/natestedman/Attributed)
44
[![License](https://img.shields.io/badge/license-Creative%20Commons%20Zero%20v1.0%20Universal-blue.svg)](https://creativecommons.org/publicdomain/zero/1.0/)
55

6-
A Swift DSL for `NSAttributedString`.
7-
8-
## Usage
9-
Attributed strings are composed through function calls on extensions of attribute value types, where the parameter is a list of attributed string values to apply the attributes to. The return values of these function calls can be included within lists passed to other function calls, creating a nested structure.
6+
A small, flexible framework for expressing `NSAttributedString` values structurally.
107

118
```swift
129
label.attributedText = [
@@ -27,25 +24,34 @@ label.attributedText = [
2724
].join().attributedString
2825
```
2926

30-
The `join` extension of `SequenceType` can be used to combine attributed strings without applying any additional attributes.
27+
## Usage
3128

32-
Any value that conforms to the `AttributedStringConvertible` protocol can be passed as an argument. Implementations for `NSAttributedString` and `String` are included, so it shouldn't be necessary to implement this type. All attribute functions return a value of this protocol type. It can be unwrapped into a `NSAttributedString` with the `attributedString` property.
29+
### Attributed String Convertibles
30+
Attributed defines the `AttributedStringConvertible` protocol, which allows clients to create an `NSAttributedString` value with the `attributedString` property. `String` and `NSAttributedString` are extended to conform to this protocol.
3331

34-
### Scoping
35-
Outer attributed values *do not* override inner values, so this code works as expected:
32+
### Attribute Functions
33+
The most important values in Attributed are _attribute functions_, which take an `AttributedStringConvertible` value as a parameter, and return a second `AttributedStringConvertible` value, with additional attributes applied. A basic attribute function can be created with the `attribute(_:withValue:)` function:
3634

3735
```swift
38-
UIColor.greenColor().foregroundAttribute([
39-
"There is some ",
40-
UIColor.blueColor().foregroundAttribute(
41-
"blue text "
42-
),
43-
"embedded in this green text."
44-
].join())
36+
let attributeFunction = attribute("Foo", withValue: "Bar")
37+
let attributedString = attributeFunction("Baz").attributedString
4538
```
4639

47-
### Attributes
48-
Attribute functions take an array of attributed string convertibles, which are joined to form an attributed string.
40+
Multiple attributes can also be set at once:
41+
42+
```swift
43+
let attributeFunction = attributesWithValues([
44+
"Foo": "Bar",
45+
"Bar": "Foo"
46+
])
47+
48+
let attributedString = attributeFunction("Baz").attributedString
49+
```
50+
51+
The `AttributeFunction` `typealias` is provided to define this function type.
52+
53+
### Extension Attribute Functions
54+
The core of Attributed's functionality is in the extensions added to support the [standard attributes](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html).
4955

5056
#### Colors
5157
`NSColor` and `UIColor` are extended with these attribute functions:
@@ -98,17 +104,58 @@ To enforce valid values for the ligature attribute, the `Ligature` type is decla
98104

99105
The `Ligature` enumeration is extended with the `attribute` function, which maps to `NSLigatureAttributeName`.
100106

101-
#### Custom Attributes
102-
Custom attributes can be added with the `attribute` and `attributes` functions.
107+
### Composition
108+
Attributed extends `SequenceType` with `join()` functions, which make flattening sequences of `AttributedStringConvertible` and `AttributeFunction` values possible.
109+
110+
```swift
111+
let attributedString = [
112+
UIColor.redColor().foregroundAttribute("Red"),
113+
UIColor.greenColor().foregroundAttribute("Green")
114+
].join().attributedString
115+
```
116+
117+
```swift
118+
let attributes = [
119+
UIColor.redColor().foregroundAttribute,
120+
UIColor.greenColor().backgroundAttribute
121+
].join()
122+
123+
let attributedString = attributes("Complementary").attributedString
124+
```
125+
126+
If you are writing an extension that composes with Attributed, it's best to work in terms of these two types. Here's an extension that applies [Photoshop tracking values](http://www.devsign.co/notes/tracking-and-character-spacing), which require knowledge of both the current font and the tracking value, and thus cannot be implemented as an `AttributeFunction`-compatible extension of either type:
103127

104128
```swift
105-
UIColor.blueColor().foregroundColor([
106-
"This is a blue string with a ",
107-
attributes(
108-
["Custom": "Value"],
109-
"custom attribute included"
129+
extension UIFont
130+
{
131+
public func photoshopTrackingAttributes(tracking: CGFloat) -> AttributeFunction
132+
{
133+
return [attribute, (pointSize * tracking / 1000).kernAttribute].join()
134+
}
135+
}
136+
```
137+
138+
This is better than a function taking the tracking value and an attributed string convertible as two arguments and returning an `AttributedStringConvertible` because it can be composed with other attribute functions:
139+
140+
```swift
141+
let attributes = [
142+
UIColor.redColor().foregroundAttribute,
143+
font.photoshopTrackingAttributes(500)
144+
].join()
145+
146+
let attributedString = attributes("Tracked Red").attributedString
147+
```
148+
149+
### Attribute Scoping
150+
Outer attributed values *do not* override inner values, so this code works as expected:
151+
152+
```swift
153+
UIColor.greenColor().foregroundAttribute([
154+
"There is some ",
155+
UIColor.blueColor().foregroundAttribute(
156+
"blue text "
110157
),
111-
"."
158+
"embedded in this green text."
112159
].join())
113160
```
114161

0 commit comments

Comments
 (0)