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
SwiftRichString is a lightweight library which allows to create and manipulate attributed strings easily both in iOS, macOS, tvOS and even watchOS.
11
10
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.
- [Apply styles to `String` & `Attributed String`](#manualstyling)
74
70
- [Fonts & Colors in `Style`](#fontscolors)
@@ -95,17 +91,13 @@ Other info:
95
91
96
92
## Introduction to `Style`, `StyleGroup`, `StyleRegEx`
97
93
98
-
<aname="introduction"/>
99
-
100
-
### Introduction
101
-
102
94
The main concept behind SwiftRichString is the use of `StyleProtocol` as generic container of the attributes you can apply to both `String` and `NSMutableAttributedString`.
103
95
Concrete classes derivated by `StyleProtocol` are: `Style`, `StyleGroup` and `StyleRegEx`.
104
96
105
97
Each of these classes can be used as source for styles you can apply to a string, substring or attributed string.
106
98
107
99
108
-
####`Style`: apply style to strings or attributed strings
100
+
### `Style`: apply style to strings or attributed strings
109
101
110
102
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.
111
103
@@ -121,7 +113,7 @@ let style = Style {
121
113
let attrString ="Some text".set(style: style) // attributed string
122
114
```
123
115
124
-
####`StyleGroup`: Apply styles for tag-based complex string
116
+
### `StyleGroup`: Apply styles for tag-based complex string
125
117
126
118
`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).
127
119
@@ -140,7 +132,7 @@ The following code defines a group where:
140
132
- 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.
141
133
142
134
143
-
####`StyleRegEx`: Apply styles via regular expressions
135
+
### `StyleRegEx`: Apply styles via regular expressions
144
136
145
137
`StyleRegEx` allows you to define a style which is applied when certain regular expression is matched inside the target string/attributed string.
146
138
@@ -156,11 +148,11 @@ let attrString = "My email is [email protected] and my website is http:/
SwiftRichString allows you to simplify string concatenation by providing custom `+` operator between `String`,`AttributedString` (typealias of `NSMutableAttributedString`) and `Style`.
Both `String` and `Attributed String` (aka `NSMutableAttributedString`) has a come convenience methods you can use to create an manipulate attributed text easily via code:
193
185
194
-
####Strings Instance Methods
186
+
### Strings Instance Methods
195
187
196
188
-`set(style: String, range: NSRange? = nil)`: apply a globally registered style to the string (or a substring) by producing an attributed string.
197
189
-`set(styles: [String], range: NSRange? = nil)`: apply an ordered sequence of globally registered styles to the string (or a substring) by producing an attributed string.
let a3 ="Hello Guys!".set(Style({ $0.font= SystemFonts.Helvetica_Bold.font(size: 20) }), range: NSMakeRange(0,4))
217
209
```
218
210
219
-
####AttributedString Instance Methods
211
+
### AttributedString Instance Methods
220
212
221
213
Similar methods are also available to attributed strings.
222
214
@@ -297,7 +289,7 @@ Clearly you can still pass instances of both colors/fonts.
297
289
298
290
<aname="derivatingstyle"/>
299
291
300
-
###Derivating a `Style`
292
+
## Derivating a `Style`
301
293
302
294
Sometimes you may need to infer properties of a new style from an existing one. In this case you can use `byAdding()` function of `Style` to produce a new style with all the properties of the receiver and the chance to configure additional/replacing attributes.
303
295
@@ -335,7 +327,7 @@ let style = Style {
335
327
336
328
<aname="xmlstrings"/>
337
329
338
-
###Render XML tagged strings
330
+
## Render XML/HTML tagged strings
339
331
340
332
SwiftRichString is also able to parse and render xml tagged strings to produce a valid `NSAttributedString` instance. This is particularly useful when you receive dynamic strings from remote services and you need to produce a rendered string easily.
###Customize XML rendering: react to tag's attributes and unknown tags
369
+
## Customize XML rendering: react to tag's attributes and unknown tags
378
370
379
371
You can also add custom attributes to your tags and render it as you prefer: you need to provide a croncrete implementation of `XMLDynamicAttributesResolver` protocol and assign it to the `StyleGroup`'s `.xmlAttributesResolver` property.
where the `b` tag's blue color was overriden by the color tag attributes and the link in 'here' is clickable.
461
453
462
454
<aname="texttransforms"/>
463
455
464
-
###Custom Text Transforms
456
+
## Custom Text Transforms
465
457
466
458
Sometimes you want to apply custom text transforms to your string; for example you may want to make some text with a given style uppercased with current locale.
467
459
In order to provide custom text transform in `Style` instances just set one or more `TextTransform` to your `Style`'s `.textTransforms` property:
@@ -496,7 +488,7 @@ All text transforms are applied in the same ordered you set in `textTransform` p
496
488
497
489
<aname="images"/>
498
490
499
-
###Local & Remote Images inside text
491
+
## Local & Remote Images inside text
500
492
501
493
SwiftRichString supports local and remote attached images along with attributed text.
502
494
You can create an attributed string with an image with a single line:
Styles can be created as you need or registered globally to be used once you need.
542
534
This second approach is strongly suggested because allows you to theme your app as you need and also avoid duplication of the code.
543
535
@@ -573,7 +565,7 @@ or you can assign `body` string to the `styledText` via Interface Builder design
573
565
574
566
<aname="defer"/>
575
567
576
-
###Defer style creation on demand
568
+
## Defer style creation on demand
577
569
Sometimes you may need to return a particular style used only in small portion of your app; while you can still set it directly you can also defer its creation in `StylesManager`.
578
570
579
571
By implementing `onDeferStyle()` callback you have an option to create a new style once required: you will receive the identifier of the style.
@@ -606,7 +598,7 @@ The following code return a valid style for `myStyle` identifier and cache it; i
606
598
607
599
Now you can use your style to render, for example, a tag based text into an `UILabel`: just set the name of the style to use.
0 commit comments