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
| 🦄 | Easy styling and typography managment with coincise declarative syntax |
17
-
| 🏞 | Attach local and remote images inside text |
17
+
| 🏞 | Attach local images (lazy/static) and remote images inside text |
18
18
| 🧬 | Fast & high customizable XML/HTML tagged string rendering |
19
19
| 🌟 | Apply text transforms within styles |
20
20
| 📐 | Native support for iOS 11 Dynamic Type |
@@ -35,7 +35,7 @@ let attributedText = "Hello World!".set(style: style) // et voilà!
35
35
```
36
36
37
37
### XML/HTML tag based rendering
38
-
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).
38
+
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 `StyleXML` (was `StyleGroup`) which allows you to encapsulate all of them and reuse as you need (clearly you can register it globally).
39
39
40
40
```swift
41
41
// Create your own styles
@@ -54,7 +54,7 @@ let italic = normal.byAdding {
54
54
$0.traitVariants= .italic
55
55
}
56
56
57
-
let myGroup =StyleGroup(base: normal, ["bold": bold, "italic": italic])
57
+
let myGroup =StyleXML(base: normal, ["bold": bold, "italic": italic])
58
58
let str ="Hello <bold>Daniele!</bold>. You're ready to <italic>play with us!</italic>"
- [Apply styles to `String` & `Attributed String`](#manualstyling)
71
71
- [Fonts & Colors in `Style`](#fontscolors)
@@ -88,12 +88,12 @@ Other info:
88
88
-[Contributing](#contributing)
89
89
-[Copyright](#copyright)
90
90
91
-
<aname="stylestylegroup"/>
91
+
<aname="styleStyleXML"/>
92
92
93
-
## Introduction to `Style`, `StyleGroup`, `StyleRegEx`
93
+
## Introduction to `Style`, `StyleXML`, `StyleRegEx`
94
94
95
95
The main concept behind SwiftRichString is the use of `StyleProtocol` as generic container of the attributes you can apply to both `String` and `NSMutableAttributedString`.
96
-
Concrete classes derivated by `StyleProtocol` are: `Style`, `StyleGroup` and `StyleRegEx`.
96
+
Concrete classes derivated by `StyleProtocol` are: `Style`, `StyleXML` and `StyleRegEx`.
97
97
98
98
Each of these classes can be used as source for styles you can apply to a string, substring or attributed string.
99
99
@@ -114,15 +114,15 @@ let style = Style {
114
114
let attrString ="Some text".set(style: style) // attributed string
115
115
```
116
116
117
-
### `StyleGroup`: Apply styles for tag-based complex string
117
+
### `StyleXML`: Apply styles for tag-based complex string
118
118
119
-
`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).
119
+
`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 `StyleXML`. You can consider a `StyleXML` 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).
120
120
121
121
```swift
122
122
let bodyStyle: Style =...
123
123
let h1Style: Style =...
124
124
let h2Style: Style =...
125
-
let group =StyleGroup(base: bodyStyle, ["h1": h1Style, "h2": h2Style])
125
+
let group =StyleXML(base: bodyStyle, ["h1": h1Style, "h2": h2Style])
126
126
127
127
let attrString ="Some <h1>text</h1>, <h2>welcome here</h2>".set(style: group)
128
128
```
@@ -200,8 +200,8 @@ Both `String` and `Attributed String` (aka `NSMutableAttributedString`) has a co
200
200
201
201
-`set(style: String, range: NSRange? = nil)`: apply a globally registered style to the string (or a substring) by producing an attributed string.
202
202
-`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.
203
-
-`set(style: StyleProtocol, range: NSRange? = nil)`: apply an instance of `Style` or `StyleGroup` (to render tag-based text) to the string (or a substring) by producting an attributed string.
204
-
-`set(styles: [StyleProtocol], range: NSRange? = nil)`: apply a sequence of `Style`/`StyleGroup` instance in order to produce a single attributes collection which will be applied to the string (or substring) to produce an attributed string.
203
+
-`set(style: StyleProtocol, range: NSRange? = nil)`: apply an instance of `Style` or `StyleXML` (to render tag-based text) to the string (or a substring) by producting an attributed string.
204
+
-`set(styles: [StyleProtocol], range: NSRange? = nil)`: apply a sequence of `Style`/`StyleXML` instance in order to produce a single attributes collection which will be applied to the string (or substring) to produce an attributed string.
let a2: AttributedString ="Hello <h1>world</h1>, <h2>welcome here</h2>".set(style: StyleXML)
219
219
220
220
// Apply a style defined via closure to a portion of the string
221
221
let a3 ="Hello Guys!".set(Style({ $0.font= SystemFonts.Helvetica_Bold.font(size: 20) }), range: NSMakeRange(0,4))
@@ -344,7 +344,7 @@ let style = Style {
344
344
345
345
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.
346
346
347
-
In order to render an XML string you need to create a compisition of all styles you are planning to render in a single `StyleGroup` instance and apply it to your source string as just you made for a single `Style`.
347
+
In order to render an XML string you need to create a compisition of all styles you are planning to render in a single `StyleXML` instance and apply it to your source string as just you made for a single `Style`.
348
348
349
349
For example:
350
350
@@ -370,7 +370,7 @@ let italicStyle = Style {
370
370
}
371
371
372
372
// A group container includes all the style defined.
373
-
let groupStyle =StyleGroup.init(base: baseStyle, ["b": boldStyle, "i": italicStyle])
373
+
let groupStyle =StyleXML.init(base: baseStyle, ["b": boldStyle, "i": italicStyle])
374
374
375
375
// We can render our string
376
376
let bodyHTML ="Hello <b>world!</b>, my name is <i>Daniele</i>"
## Customize XML rendering: react to tag's attributes and unknown tags
383
383
384
-
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.
384
+
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 `StyleXML`'s `.xmlAttributesResolver` property.
385
385
386
386
The protocol will receive two kind of events:
387
387
388
388
-`applyDynamicAttributes(to attributedString: inout AttributedString, xmlStyle: XMLDynamicStyle)` is received when parser encounter an existing style with custom attributes. Style is applied and event is called so you can make further customizations.
389
-
-`func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String: String]?)` is received when a unknown (not defined in `StyleGroup`'s styles) tag is received. You can decide to ignore or perform customizations.
389
+
-`func styleForUnknownXMLTag(_ tag: String, to attributedString: inout AttributedString, attributes: [String: String]?)` is received when a unknown (not defined in `StyleXML`'s styles) tag is received. You can decide to ignore or perform customizations.
390
390
391
391
The following example is used to override text color for when used for any known tag:
392
392
@@ -410,8 +410,8 @@ open class MyXMLDynamicAttributesResolver: XMLDynamicAttributesResolver {
410
410
}
411
411
}
412
412
413
-
// Then set it to our's StyleGroup instance before rendering text.
414
-
let groupStyle =StyleGroup.init(base: baseStyle, ["b": boldStyle, "i": italicStyle])
413
+
// Then set it to our's StyleXML instance before rendering text.
414
+
let groupStyle =StyleXML.init(base: baseStyle, ["b": boldStyle, "i": italicStyle])
Sometimes you may want to provide these images lazily. In order to do it just provide a custom implementation of the `imageProvider` callback in `StyleXML` instance:
540
+
541
+
```swift
542
+
let xmlText ="- <img named=\"check\"/> has done!"
543
+
544
+
let xmlStyle =StyleXML(base: {
545
+
/// some attributes for base style
546
+
})
547
+
548
+
// This method is called when a new `img` tag is found. It's your chance to
549
+
// return a custom image. If you return `nil` (or you don't implement this method)
550
+
// image is searched inside any bundled `xcasset` file.
Styles can be created as you need or registered globally to be used once you need.
547
571
This second approach is strongly suggested because allows you to theme your app as you need and also avoid duplication of the code.
548
572
549
-
To register a `Style` or a `StyleGroup` globally you need to assign an unique identifier to it and call `register()` function via `Styles` shortcut (which is equal to call `StylesManager.shared`).
573
+
To register a `Style` or a `StyleXML` globally you need to assign an unique identifier to it and call `register()` function via `Styles` shortcut (which is equal to call `StylesManager.shared`).
550
574
551
575
In order to keep your code type-safer you can use a non-instantiable struct to keep the name of your styles, then use it to register style:
552
576
@@ -601,7 +625,7 @@ Styles.onDeferStyle = { name in
@@ -628,10 +652,10 @@ has three additional properties:
628
652
-`style: StyleProtocol`: you can set it to render the text of the control with an instance of style instance.
629
653
-`styledText: String`: use this property, instead of `attributedText` to set a new text for the control and render it with already set style. You can continue to use `attributedText` and set the value using `.set()` functions of `String`/`AttributedString`.
630
654
631
-
Assigned style can be a `Style`, `StyleGroup` or `StyleRegEx`:
655
+
Assigned style can be a `Style`, `StyleXML` or `StyleRegEx`:
632
656
633
657
- if style is a `Style` the entire text of the control is set with the attributes defined by the style.
634
-
- if style is a `StyleGroup` a base attribute is set (if `base` is valid) and other attributes are applied once each tag is found.
658
+
- if style is a `StyleXML` a base attribute is set (if `base` is valid) and other attributes are applied once each tag is found.
635
659
- if style is a `StyleRegEx` a base attribute is set (if `base` is valid) and the attribute is applied only for matches of the specified pattern.
636
660
637
661
Typically you will set the style of a label via `Style Name` (`styleName`) property in IB and update the content of the control by setting the `styledText`:
0 commit comments