Skip to content

Commit d73a9dd

Browse files
committed
Merge branch 'release/3.0.0'
2 parents d40cd36 + 26f4611 commit d73a9dd

17 files changed

+75
-66
lines changed

Configs/SwiftRichString.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>2.0.5</string>
18+
<string>3.0.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

ExampleiOS/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414
var window: UIWindow?
1515

1616

17-
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1818
// Override point for customization after application launch.
1919
return true
2020
}

ExampleiOS/ViewController.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ class ViewController: UIViewController {
1717
override func viewDidLoad() {
1818
super.viewDidLoad()
1919

20+
/*let baseStyle = Style {
21+
$0.font = UIFont.systemFont(ofSize: self.baseFontSize)
22+
$0.lineSpacing = 2
23+
}
24+
25+
let linkStyle = Style {
26+
$0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize)
27+
$0.linkURL = URLRepresentable.tagAttribute("href")
28+
}
29+
30+
let text = "Stile <a href=\"www.facebook.com/agencearcantide/photos/a.346501409312886/346503839312643/?type=3&theater\">link</a>"
31+
let s = StyleGroup(base: baseStyle, ["a" : linkStyle])
32+
self.textView?.attributedText = text.set(style: s)
33+
*/
34+
2035
let bodyHTML = try! String(contentsOfFile: Bundle.main.path(forResource: "file", ofType: "txt")!)
2136

2237
let headerStyle = Style {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ let style = Style {
367367
SwiftRichString also support some dynamic elements in style applied by reading specific tag parameter's value.
368368
The following example render the `linkURL` property by reading the value from the source string inside `href` tag (like in real HTML text):
369369

370-
```
370+
```swift
371371
let normal = Style {
372372
$0.color = UIColor.black
373373
}

Sources/SwiftRichString/Extensions/AttributedString+Ext.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public extension AttributedString {
4848
/// - range: range of substring where style is applied, `nil` to use the entire string.
4949
/// - Returns: same instance of the receiver with - eventually - modified attributes.
5050
@discardableResult
51-
public func add(style: String, range: NSRange? = nil) -> AttributedString {
51+
func add(style: String, range: NSRange? = nil) -> AttributedString {
5252
guard let style = Styles[style] else { return self }
5353
return style.add(to: self, range: range)
5454
}
@@ -61,7 +61,7 @@ public extension AttributedString {
6161
/// - range: range of substring where style is applied, `nil` to use the entire string.
6262
/// - Returns: same instance of the receiver with - eventually - modified attributes.
6363
@discardableResult
64-
public func add(styles: [String], range: NSRange? = nil) -> AttributedString {
64+
func add(styles: [String], range: NSRange? = nil) -> AttributedString {
6565
guard let styles = Styles[styles] else { return self }
6666
return styles.mergeStyle().set(to: self, range: range)
6767
}
@@ -74,7 +74,7 @@ public extension AttributedString {
7474
/// - range: range of substring where style is applied, `nil` to use the entire string.
7575
/// - Returns: same instance of the receiver with - eventually - modified attributes.
7676
@discardableResult
77-
public func set(style: String, range: NSRange? = nil) -> AttributedString {
77+
func set(style: String, range: NSRange? = nil) -> AttributedString {
7878
guard let style = Styles[style] else { return self }
7979
return style.set(to: self, range: range)
8080
}
@@ -88,7 +88,7 @@ public extension AttributedString {
8888
/// - range: range of substring where style is applied, `nil` to use the entire string.
8989
/// - Returns: same instance of the receiver with - eventually - modified attributes.
9090
@discardableResult
91-
public func set(styles: [String], range: NSRange? = nil) -> AttributedString {
91+
func set(styles: [String], range: NSRange? = nil) -> AttributedString {
9292
guard let styles = Styles[styles] else { return self }
9393
return styles.mergeStyle().set(to: self, range: range)
9494
}
@@ -100,7 +100,7 @@ public extension AttributedString {
100100
/// - range: range of substring where style is applied, `nil` to use the entire string.
101101
/// - Returns: same instance of the receiver with - eventually - modified attributes.
102102
@discardableResult
103-
public func add(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
103+
func add(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
104104
return style.add(to: self, range: range)
105105
}
106106

@@ -112,7 +112,7 @@ public extension AttributedString {
112112
/// - range: range of substring where style is applied, `nil` to use the entire string.
113113
/// - Returns: same instance of the receiver with - eventually - modified attributes.
114114
@discardableResult
115-
public func add(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
115+
func add(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
116116
return styles.mergeStyle().add(to: self, range: range)
117117
}
118118

@@ -123,7 +123,7 @@ public extension AttributedString {
123123
/// - range: range of substring where style is applied, `nil` to use the entire string.
124124
/// - Returns: same instance of the receiver with - eventually - modified attributes.
125125
@discardableResult
126-
public func set(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
126+
func set(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
127127
return style.set(to: self, range: range)
128128
}
129129

@@ -135,7 +135,7 @@ public extension AttributedString {
135135
/// - range: range of substring where style is applied, `nil` to use the entire string.
136136
/// - Returns: same instance of the receiver with - eventually - modified attributes.
137137
@discardableResult
138-
public func set(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
138+
func set(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
139139
return styles.mergeStyle().set(to: self, range: range)
140140
}
141141

@@ -146,7 +146,7 @@ public extension AttributedString {
146146
/// - range: range of substring where style will be removed, `nil` to use the entire string.
147147
/// - Returns: same instance of the receiver with - eventually - modified attributes.
148148
@discardableResult
149-
public func removeAttributes(_ keys: [NSAttributedString.Key], range: NSRange) -> Self {
149+
func removeAttributes(_ keys: [NSAttributedString.Key], range: NSRange) -> Self {
150150
keys.forEach { self.removeAttribute($0, range: range) }
151151
return self
152152
}
@@ -155,7 +155,7 @@ public extension AttributedString {
155155
///
156156
/// - Parameter style: style to use.
157157
/// - Returns: same instance of the receiver with - eventually - modified attributes.
158-
public func remove(_ style: StyleProtocol) -> Self {
158+
func remove(_ style: StyleProtocol) -> Self {
159159
self.removeAttributes(Array(style.attributes.keys), range: NSMakeRange(0, self.length))
160160
return self
161161
}

Sources/SwiftRichString/Extensions/String+Ext.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public extension String {
4949
/// - style: name of style registered in `StylesManager` singleton.
5050
/// - range: range of substring where style is applied, `nil` to use the entire string.
5151
/// - Returns: rendered attributed string, `nil` if style is not registered.
52-
public func set(style: String, range: NSRange? = nil) -> AttributedString? {
52+
func set(style: String, range: NSRange? = nil) -> AttributedString? {
5353
return StylesManager.shared[style]?.set(to: self, range: range)
5454
}
5555

@@ -63,7 +63,7 @@ public extension String {
6363
/// - styles: ordered list of styles name to apply. Styles must be registed in `StylesManager`.
6464
/// - range: range of substring where style is applied, `nil` to use the entire string.
6565
/// - Returns: attributed string, `nil` if all specified styles required are not registered.
66-
public func set(styles: [String], range: NSRange? = nil) -> AttributedString? {
66+
func set(styles: [String], range: NSRange? = nil) -> AttributedString? {
6767
return StylesManager.shared[styles]?.mergeStyle().set(to: self, range: range)
6868
}
6969

@@ -73,7 +73,7 @@ public extension String {
7373
/// - style: style to apply.
7474
/// - range: range of substring where style is applied, `nil` to use the entire string.
7575
/// - Returns: rendered attributed string.
76-
public func set(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
76+
func set(style: StyleProtocol, range: NSRange? = nil) -> AttributedString {
7777
return style.set(to: self, range: range)
7878
}
7979

@@ -86,7 +86,7 @@ public extension String {
8686
/// - styles: ordered list of styles to apply. Styles must be registed in `StylesManager`.
8787
/// - range: range of substring where style is applied, `nil` to use the entire string.
8888
/// - Returns: attributed string.
89-
public func set(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
89+
func set(styles: [StyleProtocol], range: NSRange? = nil) -> AttributedString {
9090
return styles.mergeStyle().set(to: self, range: range)
9191
}
9292

Sources/SwiftRichString/Extensions/String+Subscript.swift

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,13 @@
3030

3131
import Foundation
3232

33-
public extension Array where Array.Element == Range<String.Index> {
34-
35-
/// Convert an array of `String.Index` to an array of `NSRange`
36-
public var nsRanges: [NSRange] {
37-
return self.map({ $0.nsRange })
38-
}
39-
40-
}
41-
42-
public extension Range where Bound == String.Index {
43-
44-
/// Return `NSRange` from standard `Range<String.Index>` range.
45-
var nsRange:NSRange {
46-
return NSRange(location: self.lowerBound.encodedOffset,
47-
length: self.upperBound.encodedOffset -
48-
self.lowerBound.encodedOffset)
49-
}
50-
}
51-
5233
public extension NSRange {
5334

5435
/// Convert receiver in String's `Range<String.Index>` for given string instance.
5536
///
5637
/// - Parameter str: source string
5738
/// - Returns: range, `nil` if invalid
58-
public func `in`(_ str: String) -> Range<String.Index>? {
39+
func `in`(_ str: String) -> Range<String.Index>? {
5940
return Range(self, in: str)
6041
}
6142

@@ -80,7 +61,7 @@ public extension String {
8061
/// - from: starting index.
8162
/// - length: length of string,
8263
/// - Returns: substring
83-
public func substring(from: Int?, length: Int) -> String? {
64+
func substring(from: Int?, length: Int) -> String? {
8465
guard length > 0 else { return nil }
8566
let start = from ?? 0
8667
let end = min(count, max(0, start) + length)
@@ -93,7 +74,7 @@ public extension String {
9374
///
9475
/// - Parameter index: index of the character
9576
/// - Returns: `Character` instance at passed index, `nil` if range is out of bounds.
96-
public subscript(index: Int) -> Character? {
77+
subscript(index: Int) -> Character? {
9778
guard !self.isEmpty, let stringIndex = self.index(startIndex, offsetBy: index, limitedBy: self.index(before: endIndex)) else { return nil }
9879
return self[stringIndex]
9980
}
@@ -103,7 +84,7 @@ public extension String {
10384
/// `String[0..<1]`
10485
///
10586
/// - Parameter value: substring
106-
public subscript(range: Range<Int>) -> Substring? {
87+
subscript(range: Range<Int>) -> Substring? {
10788
guard let left = offset(by: range.lowerBound) else { return nil }
10889
guard let right = index(left, offsetBy: range.upperBound - range.lowerBound,
10990
limitedBy: endIndex) else { return nil }
@@ -114,7 +95,7 @@ public extension String {
11495
/// `String[..<1]`
11596
///
11697
/// - Parameter value: substring
117-
public subscript(value: PartialRangeUpTo<Int>) -> Substring? {
98+
subscript(value: PartialRangeUpTo<Int>) -> Substring? {
11899
if value.upperBound < 0 {
119100
guard abs(value.upperBound) <= count else { return nil }
120101
return self[..<(count - abs(value.upperBound))]
@@ -127,7 +108,7 @@ public extension String {
127108
/// `String[...1]`
128109
///
129110
/// - Parameter range: closed subrange.
130-
public subscript(range: ClosedRange<Int>) -> Substring? {
111+
subscript(range: ClosedRange<Int>) -> Substring? {
131112
if range.upperBound < 0 {
132113
guard abs(range.lowerBound) <= count else { return nil }
133114
return self[(count - abs(range.lowerBound))...]
@@ -141,7 +122,7 @@ public extension String {
141122
/// `String[1...]`
142123
///
143124
/// - Parameter value: substring
144-
public subscript(value: PartialRangeFrom<Int>) -> Substring? {
125+
subscript(value: PartialRangeFrom<Int>) -> Substring? {
145126
guard let left = self.offset(by: value.lowerBound) else { return nil }
146127
return self[left...]
147128
}
@@ -150,7 +131,7 @@ public extension String {
150131
/// `String[...2]`
151132
///
152133
/// - Parameter value: substring
153-
public subscript(value: PartialRangeThrough<Int>) -> Substring? {
134+
subscript(value: PartialRangeThrough<Int>) -> Substring? {
154135
guard let right = self.offset(by: value.upperBound) else { return nil }
155136
return self[...right]
156137
}
@@ -164,7 +145,7 @@ public extension String {
164145
///
165146
/// - Parameter prefix: prefix to remove.
166147
/// - Returns: new instance of the string without the prefix
167-
public func removing(prefix: String) -> String {
148+
func removing(prefix: String) -> String {
168149
if hasPrefix(prefix) {
169150
let start = index(startIndex, offsetBy: prefix.count)
170151
// return substring(from: start)
@@ -173,7 +154,7 @@ public extension String {
173154
return self
174155
}
175156

176-
public func removing(suffix: String) -> String {
157+
func removing(suffix: String) -> String {
177158
if hasSuffix(suffix) {
178159
let end = index(startIndex, offsetBy: self.count-suffix.count)
179160
return self[..<end].string

Sources/SwiftRichString/Extensions/UIKit+Extras.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ extension UILabel {
6767
}
6868
}
6969

70-
/// Use this to render automatically the texct with the currently set style instance or styleName.
70+
/// Use this to render automatically the text with the currently set style instance or styleName.
7171
public var styledText: String? {
7272
get {
7373
return attributedText?.string
7474
}
7575
set {
7676
guard let text = newValue else { return }
77-
self.attributedText = self.style?.set(to: text, range: nil)
77+
let style = self.style ?? Style()
78+
self.attributedText = style.set(to: text, range: nil)
7879
}
7980
}
8081

@@ -108,14 +109,15 @@ extension UITextField {
108109
}
109110
}
110111

111-
/// Use this to render automatically the texct with the currently set style instance or styleName.
112+
/// Use this to render automatically the text with the currently set style instance or styleName.
112113
public var styledText: String? {
113114
get {
114115
return attributedText?.string
115116
}
116117
set {
117118
guard let text = newValue else { return }
118-
self.attributedText = self.style?.set(to: text, range: nil)
119+
let style = self.style ?? Style()
120+
self.attributedText = style.set(to: text, range: nil)
119121
}
120122
}
121123

@@ -149,14 +151,15 @@ extension UITextView {
149151
}
150152
}
151153

152-
/// Use this to render automatically the texct with the currently set style instance or styleName.
154+
/// Use this to render automatically the text with the currently set style instance or styleName.
153155
public var styledText: String? {
154156
get {
155157
return attributedText?.string
156158
}
157159
set {
158160
guard let text = newValue else { return }
159-
self.attributedText = self.style?.set(to: text, range: nil)
161+
let style = self.style ?? Style()
162+
self.attributedText = style.set(to: text, range: nil)
160163
}
161164
}
162165

Sources/SwiftRichString/Style/StyleGroup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class StyleGroup: StyleProtocol {
8686
private func extractParametersFromTags(_ string: String) -> [String: String]? {
8787
guard let _ = string.firstIndex(of: " ") else { return nil } // no tags
8888

89-
let pattern = "\\w*\\s*=\\s*\"?\\s*([\\w\\s%#\\/\\.;:_-]*)\\s*\"?.*?" // maybe shorter?
89+
let pattern = "\\w*\\s*=\\s*\"?\\s*([^\"][^\"]*)\\s*\"?.*?" // maybe shorter?
9090
guard let regex = try? NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators) else {
9191
return nil
9292
}
@@ -312,7 +312,7 @@ public extension Array where Array.Element == StyleProtocol {
312312
/// Merge is made in order where each n+1 elements may replace existing keys defined by n-1 elements.
313313
///
314314
/// - Returns: merged style
315-
public func mergeStyle() -> Style {
315+
func mergeStyle() -> Style {
316316
var attributes: [NSAttributedString.Key:Any] = [:]
317317
self.forEach { attributes.merge($0.attributes, uniquingKeysWith: { (_, new) in return new }) }
318318
return Style(dictionary: attributes)

Sources/SwiftRichString/Support/OrderedDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct OrderedDictionary<K: Hashable, V> {
5656
}
5757

5858
public mutating func remove(key: K) -> V? {
59-
guard let idx = self.keys.index(of: key) else { return nil }
59+
guard let idx = self.keys.firstIndex(of: key) else { return nil }
6060
self.keys.remove(at: idx)
6161
return self.dict.removeValue(forKey: key)
6262
}

0 commit comments

Comments
 (0)