1
1
# RichString
2
+
2
3
* The easiest way to work with attributed strings in Swift*
3
4
4
5
![ Swift Version 4] ( https://img.shields.io/badge/Swift-v4-yellow.svg )
9
10
[ ![ license] ( https://img.shields.io/github/license/mashape/apistatus.svg )] ( https://github.com/svdo/swift-RichString/blob/master/LICENSE )
10
11
[ ![ Build Status] ( https://travis-ci.org/svdo/swift-RichString.svg?branch=master )] ( https://travis-ci.org/svdo/swift-RichString )
11
12
12
-
13
13
## Introduction
14
- This Swift framework was built to simplify working with ` NSAttributedString ` . It does
15
- so without adding additional types; it just extends the existing types that you already have,
16
- ensuring that it is fully interoperable with any other method of using ` NSAttributedString ` .
17
14
18
- The core assumption this framework makes, is that you
19
- set up your attributed strings by first configuring
20
- all components appropriately, and then concatenating
21
- them.
15
+ This Swift framework was built to simplify working with ` NSAttributedString ` . It
16
+ does so without adding additional types; it just extends the existing types that
17
+ you already have, ensuring that it is fully interoperable with any other method
18
+ of using ` NSAttributedString ` .
19
+
20
+ The core assumption this framework makes, is that you set up your attributed
21
+ strings by first configuring all components appropriately, and then
22
+ concatenating them.
22
23
23
24
It allows you to do things like this:
24
25
@@ -31,13 +32,13 @@ let content = title + text
31
32
```
32
33
33
34
## Overview
34
- This framework provides the following primitives.
35
- There are unit tests; have a look at them for more
36
- examples.
35
+
36
+ This framework provides the following primitives. There are unit tests; have a
37
+ look at them for more examples.
37
38
38
39
### Operator ` + ` For Concatenating Attributed Strings
39
- Given two strings, you can append one to the other
40
- using the ` + ` operator:
40
+
41
+ Given two strings, you can append one to the other using the ` + ` operator:
41
42
42
43
``` swift
43
44
let s1 = NSAttributedString ()
@@ -48,37 +49,41 @@ let concatenated = s1 + s2
48
49
### ` RichString ` protocol for ` String ` , ` NSString ` and ` NSAttributedString `
49
50
50
51
#### font(_ :)
51
- Apply the given font. The type ` Font ` is a type
52
- alias of ` UIFont ` on iOS, and of ` NSFont ` on macOS.
52
+
53
+ Apply the given font. The type ` Font ` is a type alias of ` UIFont ` on iOS, and of
54
+ ` NSFont ` on macOS.
53
55
54
56
``` swift
55
57
let attributedString = " text" .font (.systemFont (ofSize : 12 ))
56
58
```
59
+
57
60
#### fontSize(_ :)
58
- Applies the given font size. If no font was set on
59
- the attributed string yet, ` Font.systemFont ` will be
60
- assumed.
61
61
62
- Note: unlike on iOS, on macOS this returns an
63
- * optional* attributed string instead.
62
+ Applies the given font size. If no font was set on the attributed string yet,
63
+ ` Font.systemFont ` will be assumed.
64
+
65
+ Note: unlike on iOS, on macOS this returns an * optional* attributed string
66
+ instead.
64
67
65
68
``` swift
66
69
let attributedString = " text" .fontSize (12 )
67
70
```
68
71
69
72
#### paragraphStyle(configure:)
70
- Applies a paragraph style, configuring it with the
71
- given closure. If the attributed string already had
72
- a paragraph style attribute, the ` configure ` closure
73
- is called on that paragraph style; otherwise a new
73
+
74
+ Applies a paragraph style, configuring it with the given closure. If the
75
+ attributed string already had a paragraph style attribute, the ` configure `
76
+ closure is called on that paragraph style; otherwise a new
74
77
` NSMutableParagraphStyle ` is used.
78
+
75
79
``` swift
76
80
let attributedString = " Hello World" .paragraphStyle {
77
81
$0 .firstLineHeadIndent = 10
78
82
}
79
83
```
80
84
81
85
#### paragraphStyle(_ :)
86
+
82
87
Applies the given paragraph style.
83
88
84
89
``` swift
@@ -88,99 +93,102 @@ let attributedString = "text".paragraphStyle(paragraphStyle)
88
93
```
89
94
90
95
#### color(_ :)
96
+
91
97
Applies the given (foreground) color.
92
98
93
99
``` swift
94
100
let attributedString = " text" .color (.blue )
95
101
```
96
102
97
103
#### backgroundColor(_ :)
104
+
98
105
Applies the given background color.
99
106
100
107
``` swift
101
108
let attributedString = " text" .backgroundColor (.yellow )
102
109
```
103
110
104
111
#### ligature(_ :)
105
- Configures whether or not to use ligatures. Default
106
- is that they are used.
112
+
113
+ Configures whether or not to use ligatures. Default is that they are used.
107
114
108
115
``` swift
109
116
let attributedString = " text" .ligature (false )
110
117
```
111
118
112
119
#### kern(_ :)
113
- Configures the amount with which to modify the
114
- default kerning. The default ` 0 ` means that no
115
- kerning change is applied.
120
+
121
+ Configures the amount with which to modify the default kerning. The default ` 0 `
122
+ means that no kerning change is applied.
116
123
117
124
``` swift
118
125
let attributedString = " text" .kern (0.1 )
119
126
```
120
127
121
128
#### strikeThrough(style:)
122
- Configures the strike through style.
123
129
124
- Please note that depending on OS and version not all
125
- styles may actually work.
130
+ Configures the strike through style. Please note that depending on OS and
131
+ version not all styles may actually work.
126
132
127
133
``` swift
128
134
let attributedString = " text" .strikeThrough (style : .styleSingle )
129
135
```
130
136
131
137
#### strikeThrough(color:)
132
- Configures the strike through color.
133
- Only setting the color has no effect, the style must
134
- be configured as well.
138
+
139
+ Configures the strike through color. Only setting the color has no effect, the
140
+ style must be configured as well.
135
141
136
142
``` swift
137
143
let attributedString = " text" .strikeThrough (color : .red )
138
144
```
139
145
140
146
#### strikeThrough(color:, style:)
141
- Configures both the strike through color and style.
142
- Please note that depending on OS and version not all
143
- styles may actually work.
147
+
148
+ Configures both the strike through color and style. Please note that depending
149
+ on OS and version not all styles may actually work.
144
150
145
151
``` swift
146
152
let attributedString = " text" .strikeThrough (color : .red , style : .styleDouble )
147
153
```
148
154
149
155
#### underline(style:)
150
- Configures the underline style.
151
- Please note that depending on OS and version not all
152
- styles may actually work.
156
+
157
+ Configures the underline style. Please note that depending on OS and version not
158
+ all styles may actually work.
153
159
154
160
``` swift
155
161
let attributedString = " text" .underline (style : .styleSingle )
156
162
```
157
163
158
164
#### underline(color:)
159
- Configures the underline color.
160
- Only setting the color has no effect, the style must
161
- be configured as well.
165
+
166
+ Configures the underline color. Only setting the color has no effect, the style
167
+ must be configured as well.
162
168
163
169
``` swift
164
170
let attributedString = " text" .underline (color : .blue )
165
171
```
166
172
167
173
#### underline(color:, style:)
168
- Configures both the underline color and style.
169
- Please note that depending on OS and version not all
170
- styles may actually work.
174
+
175
+ Configures both the underline color and style. Please note that depending on OS
176
+ and version not all styles may actually work.
171
177
172
178
``` swift
173
179
let attributedString = " text" .underline (color : .blue , style : .styleSingle )
174
180
```
175
181
176
182
#### stroke(width:, color:)
183
+
177
184
Configures the stroke.
178
185
179
186
``` swift
180
187
let attributedString = " text" .stroke (width : 2 , color : .green )
181
188
```
182
189
183
190
#### shadow(configure:)
191
+
184
192
Configures the shadow using a closure that receives
185
193
an ` NSShadow ` instance. Not available on watchOS.
186
194
@@ -193,8 +201,9 @@ let result = "Hello World".shadow {
193
201
```
194
202
195
203
#### shadow(_ :)
196
- Configures the shadow by setting an ` NSShadow `
197
- instance. Not available on watchOS.
204
+
205
+ Configures the shadow by setting an ` NSShadow ` instance. Not available on
206
+ watchOS.
198
207
199
208
``` swift
200
209
let shadow = NSShadow ()
@@ -205,33 +214,35 @@ let attributedString = "text".shadow(shadow)
205
214
```
206
215
207
216
#### letterPressed()
217
+
208
218
Adds the "letter pressed" text effect.
209
219
210
220
``` swift
211
221
let attributedString = " text" .letterPressed ()
212
222
```
213
223
214
224
#### link(url:)
215
- Creates hyperlink to the given URL with the receiver
216
- as text.
225
+
226
+ Creates hyperlink to the given URL with the receiver as text.
217
227
218
228
``` swift
219
229
let url = URL (string : " https://example.com" )
220
230
let attributedString = " text" .link (url : url)
221
231
```
222
232
223
233
#### link(string:)
224
- Creates hyperlink to the given URL with the receiver
225
- as text.
234
+
235
+ Creates hyperlink to the given URL with the receiver as text.
226
236
227
237
``` swift
228
238
let urlString = " https://example.com"
229
239
let attributedString = " text" .link (string : urlString)
230
240
```
231
241
232
242
#### attachment(configure:)
233
- Creates a new ` NSTextAttachment ` and passes it to the
234
- ` configure ` closure. Not available on watchOS.
243
+
244
+ Creates a new ` NSTextAttachment ` and passes it to the ` configure ` closure. Not
245
+ available on watchOS.
235
246
236
247
``` swift
237
248
let attributedString = NSAttributedString ().attachment {
@@ -241,20 +252,23 @@ let attributedString = NSAttributedString().attachment {
241
252
```
242
253
243
254
#### baselineOffset(_ :)
255
+
244
256
Configures the baseline offset.
245
257
246
258
``` swift
247
259
let attributedString = " text" .baselineOffset (-0.5 )
248
260
```
249
261
250
262
#### obliqueness(_ :)
263
+
251
264
Configures the skew to be applied to glyphs.
252
265
253
266
``` swift
254
267
let attributedString = " text" .obliqueness (1.5 )
255
268
```
256
269
257
270
#### expansion(_ :)
271
+
258
272
Configures the expansion to be applied to glyphs.
259
273
260
274
``` swift
@@ -263,13 +277,12 @@ let attributedString = "text".expansion(2)
263
277
264
278
### ` NSAttributedString ` Extension for Getting Attribute Values
265
279
266
- All of the above methods have corresponding getters
267
- to retrieve the attribute values from the attributed
268
- string. The all return an optional; if the attribute
269
- is not configured on the attributed string, ` nil `
270
- will be returned.
280
+ All of the above methods have corresponding getters to retrieve the attribute
281
+ values from the attributed string. The all return an optional; if the attribute
282
+ is not configured on the attributed string, ` nil ` will be returned.
271
283
272
284
The getters are:
285
+
273
286
- ` attachment: NSTextAttachment? ` (not available on watchOS)
274
287
- ` backgroundColor: Color? `
275
288
- ` baselineOffset: Float? `
@@ -291,23 +304,28 @@ The getters are:
291
304
- ` underlineStyle: NSUnderlineStyle? `
292
305
293
306
## Usage
294
- You can use the library any way you like, but two
295
- easy ways are Carthage and CocoaPods. For CocoaPods,
296
- put ` pod RichString ` in your ` Podfile ` .
307
+
308
+ You can use the library any way you like, but two easy ways are Carthage and
309
+ CocoaPods. For CocoaPods, put ` pod RichString ` in your ` Podfile ` .
297
310
298
311
## Alternative Frameworks
299
- I found a couple other frameworks that have the same goal as this one: simplifying using
300
- ` NSAttributedString ` . I like mine better, mostly because it has a simpler API that doesn't
301
- use any additional types. But I'll let you choose for yourself.
302
-
303
- [ TextAttributes] ( https://github.com/delba/TextAttributes ) is similar to this framework, but it
304
- introduces a new type that you have to set up the attributes, instead of working directly on
305
- the strings themselves. It doesn't feature the cool closure-based way of setting up
306
- ` shadow { ... } ` and ` paragraphStyle { ... } ` that this framework has. And it doesn't provide
307
- the super-convenient ` + ` operator that this framework does. Finally, I didn't test this framework
308
- on tvOS and watchOS. I'm rather confident that those will work as well, but you'll have to try.
309
- Pull requests are wellcome of course.
310
-
311
- [ TextAttributesUtil] ( https://github.com/muukii/TextAttributesUtil ) also introduces a new type
312
- for setting up the attributes. Also, it's API forces you to have more levels of nesting in your
313
- source code. It only supports iOS, not macOS.
312
+
313
+ Back when I started, I found a couple other frameworks that have the same goal
314
+ as this one: simplifying using ` NSAttributedString ` . I like mine better, mostly
315
+ because it has a simpler API that doesn't use any additional types. But I'll let
316
+ you choose for yourself. I haven't researched this anymore after that, so this
317
+ is probably outdated.
318
+
319
+ [ TextAttributes] ( https://github.com/delba/TextAttributes ) is similar to this
320
+ framework, but it introduces a new type that you have to set up the attributes,
321
+ instead of working directly on the strings themselves. It doesn't feature the
322
+ cool closure-based way of setting up ` shadow { ... } ` and `paragraphStyle { ...
323
+ }` that this framework has. And it doesn't provide the super-convenient ` +`
324
+ operator that this framework does. Finally, I didn't test this framework on tvOS
325
+ and watchOS. I'm rather confident that those will work as well, but you'll have
326
+ to try. Pull requests are wellcome of course.
327
+
328
+ [ TextAttributesUtil] ( https://github.com/muukii/TextAttributesUtil ) also
329
+ introduces a new type for setting up the attributes. Also, it's API forces you
330
+ to have more levels of nesting in your source code. It only supports iOS, not
331
+ macOS.
0 commit comments