Skip to content

Commit a37b5cb

Browse files
committed
#56 Fixed missing attributes in .attributes property of Style
Added .renderingAttributes for internal rendering.
1 parent 2bc1023 commit a37b5cb

File tree

9 files changed

+61
-26
lines changed

9 files changed

+61
-26
lines changed

.swift-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

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.attributes.keys), range: NSMakeRange(0, self.length))
159+
self.removeAttributes(Array(style.renderingAttributes.keys), range: NSMakeRange(0, self.length))
160160
return self
161161
}
162162

Sources/SwiftRichString/Style/Style.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,10 @@ public class Style: StyleProtocol {
605605
return (self.innerAttributes[key] as? T)
606606
}
607607

608-
/// Return all attributes defined by the style.
609-
public var attributes: [NSAttributedString.Key : Any] {
608+
/// Return attributes defined by the style.
609+
/// Not all attributes are returned, fonts attributes may be omitted.
610+
/// Refer to `attributes` to get the complete list.
611+
public var renderingAttributes: [NSAttributedString.Key : Any] {
610612
if let cachedAttributes = self.cachedAttributes {
611613
return cachedAttributes
612614
}
@@ -616,6 +618,15 @@ public class Style: StyleProtocol {
616618
self.cachedAttributes = self.innerAttributes.merging(fontAttributes) { (_, new) in return new }
617619
return self.cachedAttributes!
618620
}
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+
}
619630

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

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 attributes: [NSAttributedString.Key : Any] {
86+
public var renderingAttributes: [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.attributes, uniquingKeysWith: { (_, new) in return new })
91+
composedAttributes.merge(style.renderingAttributes, 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.attributes, range: range)
243+
attrStr.addAttributes(attribute.renderingAttributes, 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.attributes, uniquingKeysWith: { (_, new) in return new }) }
263+
self.forEach { attributes.merge($0.renderingAttributes, 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 attributes: [NSAttributedString.Key : Any] { get }
38+
var renderingAttributes: [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.attributes, range: (range ?? NSMakeRange(0, attributedText.length)))
59+
attributedText.addAttributes(self.renderingAttributes, 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.attributes, range: (range ?? NSMakeRange(0, source.length)))
65+
source.addAttributes(self.renderingAttributes, 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.attributes, range: (range ?? NSMakeRange(0, source.length)))
72+
source.addAttributes(self.renderingAttributes, range: (range ?? NSMakeRange(0, source.length)))
7373
return source
7474
}
7575

7676
@discardableResult
7777
func remove(from source: AttributedString, range: NSRange?) -> AttributedString {
78-
self.attributes.keys.forEach({
78+
self.renderingAttributes.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 attributes: [NSAttributedString.Key : Any] {
55-
return self.style.attributes
54+
public var renderingAttributes: [NSAttributedString.Key : Any] {
55+
return self.style.renderingAttributes
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?.attributes ?? [:]))
87+
let attributed = NSMutableAttributedString(string: source, attributes: (self.baseStyle?.renderingAttributes ?? [:]))
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.attributes, range: (range ?? NSMakeRange(0, source.length)))
93+
source.addAttributes(base.renderingAttributes, 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.attributes, range: (range ?? NSMakeRange(0, source.length)))
100+
source.setAttributes(base.renderingAttributes, 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.attributes, range: r.range)
122+
str.addAttributes(self.renderingAttributes, range: r.range)
123123
} else {
124-
str.setAttributes(self.attributes, range: r.range)
124+
str.setAttributes(self.renderingAttributes, range: r.range)
125125
}
126126
}
127127
}

SwiftRichString.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "SwiftRichString"
3-
s.version = "2.0.6"
3+
s.version = "2.1.0"
44
s.summary = "Elegant Strings & Attributed Strings Toolkit for Swift"
55
s.description = <<-DESC
66
SwiftRichString is the best toolkit to work easily with Strings and Attributed Strings.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
ignoreCount = "0"
1111
continueAfterRunningActions = "No"
1212
filePath = "Sources/SwiftRichString/Style/Style.swift"
13-
timestampString = "548505331.301"
13+
timestampString = "569605751.212633"
1414
startingColumnNumber = "9223372036854775807"
1515
endingColumnNumber = "9223372036854775807"
16-
startingLineNumber = "300"
17-
endingLineNumber = "300"
18-
landmarkName = "baseWritingDirection"
19-
landmarkType = "24">
16+
startingLineNumber = "302"
17+
endingLineNumber = "302"
18+
landmarkName = "Style"
19+
landmarkType = "3">
2020
</BreakpointContent>
2121
</BreakpointProxy>
2222
<BreakpointProxy

SwiftRichString.xcodeproj/xcuserdata/daniele.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,51 @@
99
<key>orderHint</key>
1010
<integer>5</integer>
1111
</dict>
12+
<key>ExampleMac.xcscheme_^#shared#^_</key>
13+
<dict>
14+
<key>orderHint</key>
15+
<integer>5</integer>
16+
</dict>
1217
<key>ExampleTvOS.xcscheme</key>
1318
<dict>
1419
<key>orderHint</key>
1520
<integer>6</integer>
1621
</dict>
22+
<key>ExampleTvOS.xcscheme_^#shared#^_</key>
23+
<dict>
24+
<key>orderHint</key>
25+
<integer>6</integer>
26+
</dict>
1727
<key>ExampleWatchOS (Notification).xcscheme</key>
1828
<dict>
1929
<key>orderHint</key>
2030
<integer>8</integer>
2131
</dict>
32+
<key>ExampleWatchOS (Notification).xcscheme_^#shared#^_</key>
33+
<dict>
34+
<key>orderHint</key>
35+
<integer>8</integer>
36+
</dict>
2237
<key>ExampleWatchOS.xcscheme</key>
2338
<dict>
2439
<key>orderHint</key>
2540
<integer>7</integer>
2641
</dict>
42+
<key>ExampleWatchOS.xcscheme_^#shared#^_</key>
43+
<dict>
44+
<key>orderHint</key>
45+
<integer>7</integer>
46+
</dict>
2747
<key>ExampleiOS.xcscheme</key>
2848
<dict>
2949
<key>orderHint</key>
3050
<integer>4</integer>
3151
</dict>
52+
<key>ExampleiOS.xcscheme_^#shared#^_</key>
53+
<dict>
54+
<key>orderHint</key>
55+
<integer>4</integer>
56+
</dict>
3257
<key>SwiftRichString-iOS.xcscheme_^#shared#^_</key>
3358
<dict>
3459
<key>orderHint</key>

0 commit comments

Comments
 (0)