Skip to content

Commit 13d217e

Browse files
committed
#56 Minor refactoring
1 parent a37b5cb commit 13d217e

File tree

8 files changed

+36
-31
lines changed

8 files changed

+36
-31
lines changed

ExampleiOS/ViewController.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@ class ViewController: UIViewController {
1616

1717
override func viewDidLoad() {
1818
super.viewDidLoad()
19+
20+
let patternText = "[A-Z0-9a-z._%+]+@[A-Za-z0-9.]+\\.[A-Za-z]{2,4}"
21+
let bodyHTML = "daniele [email protected] ciao ciao"
22+
23+
let baseStyle = Style {
24+
$0.font = UIFont.boldSystemFont(ofSize: 12)
25+
}
26+
27+
let highlightedStyle: StyleRegEx = StyleRegEx(base: baseStyle, pattern: patternText) {
28+
$0.font = UIFont.systemFont(ofSize: 40, weight: .bold)
29+
$0.color = UIColor.red
30+
}!
31+
self.textView?.attributedText = bodyHTML.set(style: highlightedStyle)
32+
1933

20-
let bodyHTML = try! String(contentsOfFile: Bundle.main.path(forResource: "file", ofType: "txt")!)
34+
/*let bodyHTML = try! String(contentsOfFile: Bundle.main.path(forResource: "file", ofType: "txt")!)
2135

2236
let headerStyle = Style {
2337
$0.font = UIFont.boldSystemFont(ofSize: self.baseFontSize * 1.15)
@@ -63,6 +77,6 @@ class ViewController: UIViewController {
6377
self.textView?.attributedText = bodyHTML.set(style: style)
6478
if #available(iOS 10.0, *) {
6579
self.textView?.adjustsFontForContentSizeCategory = true
66-
}
80+
}*/
6781
}
6882
}

Sources/SwiftRichString/Attributes/FontData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class FontData {
109109
/// - Parameter size: ignored. It will be overriden by `fontSize` property.
110110
/// - Returns: instance of the font
111111
var attributes: [NSAttributedString.Key:Any] {
112-
guard !self.explicitFont else {
112+
guard self.explicitFont == true else {
113113
return [:]
114114
}
115115
return attributes(currentFont: self.font, size: self.size)

Sources/SwiftRichString/Extensions/AttributedString+Ext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public extension AttributedString {
156156
/// - Parameter style: style to use.
157157
/// - Returns: same instance of the receiver with - eventually - modified attributes.
158158
public func remove(_ style: StyleProtocol) -> Self {
159-
self.removeAttributes(Array(style.renderingAttributes.keys), range: NSMakeRange(0, self.length))
159+
self.removeAttributes(Array(style.attributes.keys), range: NSMakeRange(0, self.length))
160160
return self
161161
}
162162

Sources/SwiftRichString/Style/Style.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ public class Style: StyleProtocol {
608608
/// Return attributes defined by the style.
609609
/// Not all attributes are returned, fonts attributes may be omitted.
610610
/// Refer to `attributes` to get the complete list.
611-
public var renderingAttributes: [NSAttributedString.Key : Any] {
611+
public var attributes: [NSAttributedString.Key : Any] {
612612
if let cachedAttributes = self.cachedAttributes {
613613
return cachedAttributes
614614
}
@@ -618,15 +618,6 @@ public class Style: StyleProtocol {
618618
self.cachedAttributes = self.innerAttributes.merging(fontAttributes) { (_, new) in return new }
619619
return self.cachedAttributes!
620620
}
621-
622-
/// Return attributes defined by the style.
623-
public var attributes: [NSAttributedString.Key : Any] {
624-
var allAttributes = self.renderingAttributes
625-
if let fontRawAttributes = self.fontData?.attributes(currentFont: self.fontData?.font, size: self.fontData?.size) {
626-
allAttributes.merge(fontRawAttributes, uniquingKeysWith: { (_, new) in return new })
627-
}
628-
return allAttributes
629-
}
630621

631622
/// Create a new style copy of `self` with the opportunity to configure it via configuration callback.
632623
///

Sources/SwiftRichString/Style/StyleGroup.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ public class StyleGroup: StyleProtocol {
8383

8484
/// Return all attributes merge of each single `Style` of the group.
8585
/// Attributes are reported in order of the insertion regardeless the associated name.
86-
public var renderingAttributes: [NSAttributedString.Key : Any] {
86+
public var attributes: [NSAttributedString.Key : Any] {
8787
var composedAttributes: [NSAttributedString.Key: Any] = [:]
8888
self.styles.enumerated().forEach { (arg) in
8989

9090
let (_, style) = arg
91-
composedAttributes.merge(style.renderingAttributes, uniquingKeysWith: { (_, new) in return new })
91+
composedAttributes.merge(style.attributes, uniquingKeysWith: { (_, new) in return new })
9292
}
9393
return composedAttributes
9494
}
@@ -240,7 +240,7 @@ public class StyleGroup: StyleProtocol {
240240
let length = closingTag.range.location-location
241241
let range = NSRange(location: location, length: length)
242242
attribute.fontData?.addAttributes(to: attrStr, range: range)
243-
attrStr.addAttributes(attribute.renderingAttributes, range: range)
243+
attrStr.addAttributes(attribute.attributes, range: range)
244244
}
245245
}
246246

@@ -260,7 +260,7 @@ public extension Array where Array.Element == StyleProtocol {
260260
/// - Returns: merged style
261261
public func mergeStyle() -> Style {
262262
var attributes: [NSAttributedString.Key:Any] = [:]
263-
self.forEach { attributes.merge($0.renderingAttributes, uniquingKeysWith: { (_, new) in return new }) }
263+
self.forEach { attributes.merge($0.attributes, uniquingKeysWith: { (_, new) in return new }) }
264264
return Style(dictionary: attributes)
265265
}
266266

Sources/SwiftRichString/Style/StyleProtocol.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public typealias AttributedString = NSMutableAttributedString
3535
public protocol StyleProtocol: class {
3636

3737
/// Return the attributes of the style in form of dictionary `NSAttributedStringKey`/`Any`.
38-
var renderingAttributes: [NSAttributedString.Key : Any] { get }
38+
var attributes: [NSAttributedString.Key : Any] { get }
3939

4040
/// Font unique attributes dictionary.
4141
var fontData: FontData? { get }
@@ -56,26 +56,26 @@ public extension StyleProtocol {
5656
func set(to source: String, range: NSRange?) -> AttributedString {
5757
let attributedText = NSMutableAttributedString(string: source)
5858
self.fontData?.addAttributes(to: attributedText, range: nil)
59-
attributedText.addAttributes(self.renderingAttributes, range: (range ?? NSMakeRange(0, attributedText.length)))
59+
attributedText.addAttributes(self.attributes, range: (range ?? NSMakeRange(0, attributedText.length)))
6060
return attributedText
6161
}
6262

6363
func add(to source: AttributedString, range: NSRange?) -> AttributedString {
6464
self.fontData?.addAttributes(to: source, range: range)
65-
source.addAttributes(self.renderingAttributes, range: (range ?? NSMakeRange(0, source.length)))
65+
source.addAttributes(self.attributes, range: (range ?? NSMakeRange(0, source.length)))
6666
return source
6767
}
6868

6969
@discardableResult
7070
func set(to source: AttributedString, range: NSRange?) -> AttributedString {
7171
self.fontData?.addAttributes(to: source, range: range)
72-
source.addAttributes(self.renderingAttributes, range: (range ?? NSMakeRange(0, source.length)))
72+
source.addAttributes(self.attributes, range: (range ?? NSMakeRange(0, source.length)))
7373
return source
7474
}
7575

7676
@discardableResult
7777
func remove(from source: AttributedString, range: NSRange?) -> AttributedString {
78-
self.renderingAttributes.keys.forEach({
78+
self.attributes.keys.forEach({
7979
source.removeAttribute($0, range: (range ?? NSMakeRange(0, source.length)))
8080
})
8181
return source

Sources/SwiftRichString/Style/StyleRegEx.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public class StyleRegEx: StyleProtocol {
5151
private var style: StyleProtocol
5252

5353
/// Style attributes
54-
public var renderingAttributes: [NSAttributedString.Key : Any] {
55-
return self.style.renderingAttributes
54+
public var attributes: [NSAttributedString.Key : Any] {
55+
return self.style.attributes
5656
}
5757

5858
/// Font attributes
@@ -84,20 +84,20 @@ public class StyleRegEx: StyleProtocol {
8484
//MARK: - METHOD OVERRIDES
8585

8686
public func set(to source: String, range: NSRange?) -> AttributedString {
87-
let attributed = NSMutableAttributedString(string: source, attributes: (self.baseStyle?.renderingAttributes ?? [:]))
87+
let attributed = NSMutableAttributedString(string: source, attributes: (self.baseStyle?.attributes ?? [:]))
8888
return self.applyStyle(to: attributed, add: false, range: range)
8989
}
9090

9191
public func add(to source: AttributedString, range: NSRange?) -> AttributedString {
9292
if let base = self.baseStyle {
93-
source.addAttributes(base.renderingAttributes, range: (range ?? NSMakeRange(0, source.length)))
93+
source.addAttributes(base.attributes, range: (range ?? NSMakeRange(0, source.length)))
9494
}
9595
return self.applyStyle(to: source, add: true, range: range)
9696
}
9797

9898
public func set(to source: AttributedString, range: NSRange?) -> AttributedString {
9999
if let base = self.baseStyle {
100-
source.setAttributes(base.renderingAttributes, range: (range ?? NSMakeRange(0, source.length)))
100+
source.setAttributes(base.attributes, range: (range ?? NSMakeRange(0, source.length)))
101101
}
102102
return self.applyStyle(to: source, add: false, range: range)
103103
}
@@ -119,9 +119,9 @@ public class StyleRegEx: StyleProtocol {
119119
(result : NSTextCheckingResult?, _, _) in
120120
if let r = result {
121121
if add {
122-
str.addAttributes(self.renderingAttributes, range: r.range)
122+
str.addAttributes(self.attributes, range: r.range)
123123
} else {
124-
str.setAttributes(self.renderingAttributes, range: r.range)
124+
str.setAttributes(self.attributes, range: r.range)
125125
}
126126
}
127127
}

SwiftRichString.xcodeproj/xcuserdata/daniele.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ignoreCount = "0"
1111
continueAfterRunningActions = "No"
1212
filePath = "Sources/SwiftRichString/Style/Style.swift"
13-
timestampString = "569605751.212633"
13+
timestampString = "569606555.43208"
1414
startingColumnNumber = "9223372036854775807"
1515
endingColumnNumber = "9223372036854775807"
1616
startingLineNumber = "302"

0 commit comments

Comments
 (0)