Skip to content

Commit f1cacc8

Browse files
authored
Merge pull request #28 from eddiekaiger/swift-4.2-support
Swift 4.2 Support
2 parents 0e0ccb9 + 8e2bffd commit f1cacc8

10 files changed

+98
-66
lines changed

SwiftyAttributes.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Pod::Spec.new do |s|
33

44
s.name = "SwiftyAttributes"
5-
s.version = "4.2.0"
5+
s.version = "4.3.0"
66
s.summary = "A Swifty API for attributed strings."
77

88
s.description = <<-DESC

SwiftyAttributes/Sources/common/Attribute+Sequence.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
An extension on dictionaries that allows us to convert a Foundation-based dictionary of attributes to an array of `Attribute`s.
1111
*/
1212
#if swift(>=4.0)
13-
extension Dictionary where Key == NSAttributedStringKey {
13+
extension Dictionary where Key == AttributeName {
1414

1515
/// Returns an array of `Attribute`s converted from the dictionary of attributes. Use this whenever you want to convert [NSAttributeStringKey: Any] to [Attribute].
1616
public var swiftyAttributes: [Attribute] {

SwiftyAttributes/Sources/common/Attribute.swift

+17-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public typealias Shadow = NSShadow
2828
public typealias TextAttachment = NSTextAttachment
2929
#endif
3030

31-
#if swift(>=4.0)
31+
#if swift(>=4.2)
32+
public typealias AttributeName = NSAttributedString.Key
33+
#elseif swift(>=4.0)
3234
public typealias AttributeName = NSAttributedStringKey
3335
#else
3436
public typealias AttributeName = Attribute.Name
@@ -188,11 +190,23 @@ public enum Attribute {
188190
case .strokeColor: ret = .strokeColor(validate(foundationValue))
189191
case .strokeWidth: ret = .strokeWidth(validateDouble(foundationValue))
190192
case .strikethroughColor: ret = .strikethroughColor(validate(foundationValue))
191-
case .strikethroughStyle: ret = .strikethroughStyle(StrikethroughStyle(rawValue: validate(foundationValue))!)
193+
case .strikethroughStyle:
194+
#if swift(>=4.2)
195+
let style = StrikethroughStyle(rawValue: validate(foundationValue))
196+
#else
197+
let style = StrikethroughStyle(rawValue: validate(foundationValue))!
198+
#endif
199+
ret = .strikethroughStyle(style)
192200
case .foregroundColor: ret = .textColor(validate(foundationValue))
193201
case .textEffect: ret = .textEffect(TextEffect(rawValue: validate(foundationValue))!)
194202
case .underlineColor: ret = .underlineColor(validate(foundationValue))
195-
case .underlineStyle: ret = .underlineStyle(UnderlineStyle(rawValue: validate(foundationValue))!)
203+
case .underlineStyle:
204+
#if swift(>=4.2)
205+
let style = UnderlineStyle(rawValue: validate(foundationValue))
206+
#else
207+
let style = UnderlineStyle(rawValue: validate(foundationValue))!
208+
#endif
209+
ret = .underlineStyle(style)
196210
case .verticalGlyphForm: ret = .verticalGlyphForm(VerticalGlyphForm(rawValue: validate(foundationValue))!)
197211
case .writingDirection:
198212
let values: [Int] = validate(foundationValue)

SwiftyAttributes/Sources/common/NSAttributedString+SwiftyAttributes.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
#if swift(>=4.0)
12-
public typealias StringKey = NSAttributedStringKey
12+
public typealias StringKey = AttributeName
1313
#else
1414
public typealias StringKey = String
1515
#endif

SwiftyAttributesTests/Attribute+Sequence_Tests.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,47 @@ class Attribute_Sequence_Tests: XCTestCase {
1313

1414
func testDictionaryToSwiftyAttributes() {
1515
#if swift(>=4.0)
16-
let dict: [NSAttributedStringKey: Any] = [
16+
let dict: [AttributeName: Any] = [
1717
.baselineOffset: 3.2,
1818
.expansion: 5,
19-
.underlineStyle: NSUnderlineStyle.styleDouble.rawValue
19+
.foregroundColor: Color.red
2020
]
2121
let sort: (Attribute, Attribute) -> Bool = { $0.keyName.rawValue < $1.keyName.rawValue }
2222
#else
2323
let dict: [String: Any] = [
2424
NSBaselineOffsetAttributeName: 3.2,
2525
NSExpansionAttributeName: 5,
26-
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue
26+
NSForegroundColorAttributeName: Color.red
2727
]
2828
let sort: (Attribute, Attribute) -> Bool = { $0.0.keyName < $0.1.keyName }
2929
#endif
3030
let expected: [Attribute] = [
3131
.baselineOffset(3.2),
3232
.expansion(5),
33-
.underlineStyle(.styleDouble)
33+
.textColor(.red)
3434
].sorted(by: sort)
3535
XCTAssertEqual(dict.swiftyAttributes.sorted(by: sort), expected)
3636
}
3737

3838
func testDictionaryToSwiftyAttributes_withCustomValues() {
3939
#if swift(>=4.0)
40-
let dict: [NSAttributedStringKey: Any] = [
40+
let dict: [AttributeName: Any] = [
4141
.baselineOffset: 3.2,
42-
.underlineStyle: NSUnderlineStyle.styleDouble.rawValue,
42+
.foregroundColor: Color.red,
4343
.init("Foo"): 5
4444
]
4545
let sort: (Attribute, Attribute) -> Bool = { $0.keyName.rawValue < $1.keyName.rawValue }
4646
#else
4747
let dict: [String: Any] = [
4848
NSBaselineOffsetAttributeName: 3.2,
49-
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue,
49+
NSForegroundColorAttributeName: Color.red,
5050
"Foo": 5
5151
]
5252
let sort: (Attribute, Attribute) -> Bool = { $0.0.keyName < $0.1.keyName }
5353
#endif
5454
let expected: [Attribute] = [
5555
.baselineOffset(3.2),
56-
.underlineStyle(.styleDouble),
56+
.textColor(.red),
5757
.custom("Foo", 5)
5858
].sorted(by: sort)
5959
XCTAssertEqual(dict.swiftyAttributes.sorted(by: sort), expected)
@@ -66,7 +66,7 @@ class Attribute_Sequence_Tests: XCTestCase {
6666
.custom("Foo", 42)
6767
]
6868
#if swift(>=4.0)
69-
let expected: [NSAttributedStringKey: Any] = [
69+
let expected: [AttributeName: Any] = [
7070
.kern: 3.5,
7171
.link: URL(string: "www.google.com")!,
7272
.init(rawValue: "Foo"): 42

SwiftyAttributesTests/NSAttributedString_Tests.swift

+17-6
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,26 @@ class NSAttributedString_Tests: XCTestCase {
212212
}
213213

214214
func testAttributeAtLocation_strikethroughStyle() {
215-
let str = "Hello".withStrikethroughStyle(.styleDouble)
215+
let style: NSUnderlineStyle
216+
#if swift(>=4.2)
217+
style = .double
218+
#else
219+
style = .styleDouble
220+
#endif
221+
222+
let str = "Hello".withStrikethroughStyle(style)
216223
let subject = str.swiftyAttribute(.strikethroughStyle, at: 0, effectiveRange: nil)!.value as! StrikethroughStyle
217224
#if swift(>=4.0)
218225
let expected = str.attribute(.strikethroughStyle, at: 0, effectiveRange: nil) as! Int
219226
#else
220227
let expected = str.attribute(NSStrikethroughStyleAttributeName, at: 0, effectiveRange: nil) as! Int
221228
#endif
222229
XCTAssertEqual(subject.rawValue, expected)
223-
XCTAssertEqual(subject, .styleDouble)
230+
#if swift(>=4.2)
231+
XCTAssertEqual(subject, .double)
232+
#else
233+
XCTAssertEqual(subject, .styleDouble)
234+
#endif
224235
}
225236

226237
func testAttributeAtLocation_textColor() {
@@ -441,7 +452,7 @@ class NSAttributedString_Tests: XCTestCase {
441452
func testFontAttributes_onlyFontAttributes() {
442453
let subject = "Hello".withFont(.systemFont(ofSize: 19)).fontAttributes(in: 0 ..< 3).foundationAttributes
443454
#if swift(>=4.0)
444-
let attributeName = NSAttributedStringKey.font
455+
let attributeName = AttributeName.font
445456
#else
446457
let attributeName = NSFontAttributeName
447458
#endif
@@ -454,8 +465,8 @@ class NSAttributedString_Tests: XCTestCase {
454465
let url = URL(string: "www.google.com")!
455466
let subject = "Hello".withAttributes([.font(.systemFont(ofSize: 19)), .link(url)]).fontAttributes(in: 0 ..< 3).foundationAttributes
456467
#if swift(>=4.0)
457-
let fontAttributeName = NSAttributedStringKey.font
458-
let linkAttributeName = NSAttributedStringKey.link
468+
let fontAttributeName = AttributeName.font
469+
let linkAttributeName = AttributeName.link
459470
#else
460471
let fontAttributeName = NSFontAttributeName
461472
let linkAttributeName = NSLinkAttributeName
@@ -472,7 +483,7 @@ class NSAttributedString_Tests: XCTestCase {
472483
style.alignment = .center
473484
let subject = "Hello".withParagraphStyle(style).rulerAttributes(in: 1 ..< 3).foundationAttributes
474485
#if swift(>=4.0)
475-
let attributeName = NSAttributedStringKey.paragraphStyle
486+
let attributeName = AttributeName.paragraphStyle
476487
#else
477488
let attributeName = NSParagraphStyleAttributeName
478489
#endif

SwiftyAttributesTests/NSMutableAttributedString_Tests.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
1818
subject.addAttributes([.link(URL(string: "https://google.com")!)], range: 1 ..< 3)
1919
XCTAssertNotEqual(subject, expected)
2020
#if swift(>=4.0)
21-
let linkAttributeName = NSAttributedStringKey.link
21+
let linkAttributeName = AttributeName.link
2222
#else
2323
let linkAttributeName = NSLinkAttributeName
2424
#endif
@@ -33,7 +33,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
3333
subject.addAttributes([.font(.boldSystemFont(ofSize: 17))], range: NSRange(location: 0, length: 1))
3434
XCTAssertNotEqual(subject, expected)
3535
#if swift(>=4.0)
36-
let fontAttributeName = NSAttributedStringKey.font
36+
let fontAttributeName = AttributeName.font
3737
#else
3838
let fontAttributeName = NSFontAttributeName
3939
#endif
@@ -48,7 +48,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
4848
subject.setAttributes([.backgroundColor(.orange)], range: 0 ..< 3)
4949
XCTAssertNotEqual(subject, expected)
5050
#if swift(>=4.0)
51-
let attributeName = NSAttributedStringKey.backgroundColor
51+
let attributeName = AttributeName.backgroundColor
5252
#else
5353
let attributeName = NSBackgroundColorAttributeName
5454
#endif
@@ -63,7 +63,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
6363
subject.setAttributes([.backgroundColor(.orange)], range: NSRange(location: 2, length: 2))
6464
XCTAssertNotEqual(subject, expected)
6565
#if swift(>=4.0)
66-
let attributeName = NSAttributedStringKey.backgroundColor
66+
let attributeName = AttributeName.backgroundColor
6767
#else
6868
let attributeName = NSBackgroundColorAttributeName
6969
#endif
@@ -88,7 +88,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
8888
subject.replaceCharacters(in: 0 ..< 2, with: "Chi".withBackgroundColor(.magenta))
8989
XCTAssertNotEqual(subject, expected)
9090
#if swift(>=4.0)
91-
let attributeName = NSAttributedStringKey.backgroundColor
91+
let attributeName = AttributeName.backgroundColor
9292
#else
9393
let attributeName = NSBackgroundColorAttributeName
9494
#endif
@@ -113,7 +113,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
113113
subject.removeAttribute(.baselineOffset, range: 1 ..< 4)
114114
XCTAssertNotEqual(subject, expected)
115115
#if swift(>=4.0)
116-
let attributeName = NSAttributedStringKey.baselineOffset
116+
let attributeName = AttributeName.baselineOffset
117117
#else
118118
let attributeName = NSBaselineOffsetAttributeName
119119
#endif

SwiftyAttributesTests/Operators_Tests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class Operators_Tests: XCTestCase {
1616
let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24))
1717
let newString = lhs + rhs
1818
#if swift(>=4.0)
19-
let leftAttributes: [NSAttributedStringKey: NSObject] = [.font: Font.systemFont(ofSize: 19)]
19+
let leftAttributes: [AttributeName: NSObject] = [.font: Font.systemFont(ofSize: 19)]
2020
XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary)
21-
let rightAttributes: [NSAttributedStringKey: NSObject] = [
21+
let rightAttributes: [AttributeName: NSObject] = [
2222
.foregroundColor: Color.magenta,
2323
.backgroundColor: Color.orange,
2424
.font: Font.boldSystemFont(ofSize: 24)

0 commit comments

Comments
 (0)