You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
7
11
8
```swift
12
9
label.attributedText= [
@@ -27,25 +24,34 @@ label.attributedText = [
27
24
].join().attributedString
28
25
```
29
26
30
-
The `join` extension of `SequenceType` can be used to combine attributed strings without applying any additional attributes.
27
+
## Usage
31
28
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.
33
31
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:
36
34
37
35
```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
45
38
```
46
39
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).
49
55
50
56
#### Colors
51
57
`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
98
104
99
105
The `Ligature` enumeration is extended with the `attribute` function, which maps to `NSLigatureAttributeName`.
100
106
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:
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:
0 commit comments