Skip to content

Commit

Permalink
Merge pull request eddiekaiger#28 from eddiekaiger/swift-4.2-support
Browse files Browse the repository at this point in the history
Swift 4.2 Support
  • Loading branch information
eddiekaiger authored Jun 5, 2018
2 parents 0e0ccb9 + 8e2bffd commit f1cacc8
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 66 deletions.
2 changes: 1 addition & 1 deletion SwiftyAttributes.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Pod::Spec.new do |s|

s.name = "SwiftyAttributes"
s.version = "4.2.0"
s.version = "4.3.0"
s.summary = "A Swifty API for attributed strings."

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion SwiftyAttributes/Sources/common/Attribute+Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
An extension on dictionaries that allows us to convert a Foundation-based dictionary of attributes to an array of `Attribute`s.
*/
#if swift(>=4.0)
extension Dictionary where Key == NSAttributedStringKey {
extension Dictionary where Key == AttributeName {

/// Returns an array of `Attribute`s converted from the dictionary of attributes. Use this whenever you want to convert [NSAttributeStringKey: Any] to [Attribute].
public var swiftyAttributes: [Attribute] {
Expand Down
20 changes: 17 additions & 3 deletions SwiftyAttributes/Sources/common/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public typealias Shadow = NSShadow
public typealias TextAttachment = NSTextAttachment
#endif

#if swift(>=4.0)
#if swift(>=4.2)
public typealias AttributeName = NSAttributedString.Key
#elseif swift(>=4.0)
public typealias AttributeName = NSAttributedStringKey
#else
public typealias AttributeName = Attribute.Name
Expand Down Expand Up @@ -188,11 +190,23 @@ public enum Attribute {
case .strokeColor: ret = .strokeColor(validate(foundationValue))
case .strokeWidth: ret = .strokeWidth(validateDouble(foundationValue))
case .strikethroughColor: ret = .strikethroughColor(validate(foundationValue))
case .strikethroughStyle: ret = .strikethroughStyle(StrikethroughStyle(rawValue: validate(foundationValue))!)
case .strikethroughStyle:
#if swift(>=4.2)
let style = StrikethroughStyle(rawValue: validate(foundationValue))
#else
let style = StrikethroughStyle(rawValue: validate(foundationValue))!
#endif
ret = .strikethroughStyle(style)
case .foregroundColor: ret = .textColor(validate(foundationValue))
case .textEffect: ret = .textEffect(TextEffect(rawValue: validate(foundationValue))!)
case .underlineColor: ret = .underlineColor(validate(foundationValue))
case .underlineStyle: ret = .underlineStyle(UnderlineStyle(rawValue: validate(foundationValue))!)
case .underlineStyle:
#if swift(>=4.2)
let style = UnderlineStyle(rawValue: validate(foundationValue))
#else
let style = UnderlineStyle(rawValue: validate(foundationValue))!
#endif
ret = .underlineStyle(style)
case .verticalGlyphForm: ret = .verticalGlyphForm(VerticalGlyphForm(rawValue: validate(foundationValue))!)
case .writingDirection:
let values: [Int] = validate(foundationValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

#if swift(>=4.0)
public typealias StringKey = NSAttributedStringKey
public typealias StringKey = AttributeName
#else
public typealias StringKey = String
#endif
Expand Down
18 changes: 9 additions & 9 deletions SwiftyAttributesTests/Attribute+Sequence_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,47 @@ class Attribute_Sequence_Tests: XCTestCase {

func testDictionaryToSwiftyAttributes() {
#if swift(>=4.0)
let dict: [NSAttributedStringKey: Any] = [
let dict: [AttributeName: Any] = [
.baselineOffset: 3.2,
.expansion: 5,
.underlineStyle: NSUnderlineStyle.styleDouble.rawValue
.foregroundColor: Color.red
]
let sort: (Attribute, Attribute) -> Bool = { $0.keyName.rawValue < $1.keyName.rawValue }
#else
let dict: [String: Any] = [
NSBaselineOffsetAttributeName: 3.2,
NSExpansionAttributeName: 5,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue
NSForegroundColorAttributeName: Color.red
]
let sort: (Attribute, Attribute) -> Bool = { $0.0.keyName < $0.1.keyName }
#endif
let expected: [Attribute] = [
.baselineOffset(3.2),
.expansion(5),
.underlineStyle(.styleDouble)
.textColor(.red)
].sorted(by: sort)
XCTAssertEqual(dict.swiftyAttributes.sorted(by: sort), expected)
}

func testDictionaryToSwiftyAttributes_withCustomValues() {
#if swift(>=4.0)
let dict: [NSAttributedStringKey: Any] = [
let dict: [AttributeName: Any] = [
.baselineOffset: 3.2,
.underlineStyle: NSUnderlineStyle.styleDouble.rawValue,
.foregroundColor: Color.red,
.init("Foo"): 5
]
let sort: (Attribute, Attribute) -> Bool = { $0.keyName.rawValue < $1.keyName.rawValue }
#else
let dict: [String: Any] = [
NSBaselineOffsetAttributeName: 3.2,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue,
NSForegroundColorAttributeName: Color.red,
"Foo": 5
]
let sort: (Attribute, Attribute) -> Bool = { $0.0.keyName < $0.1.keyName }
#endif
let expected: [Attribute] = [
.baselineOffset(3.2),
.underlineStyle(.styleDouble),
.textColor(.red),
.custom("Foo", 5)
].sorted(by: sort)
XCTAssertEqual(dict.swiftyAttributes.sorted(by: sort), expected)
Expand All @@ -66,7 +66,7 @@ class Attribute_Sequence_Tests: XCTestCase {
.custom("Foo", 42)
]
#if swift(>=4.0)
let expected: [NSAttributedStringKey: Any] = [
let expected: [AttributeName: Any] = [
.kern: 3.5,
.link: URL(string: "www.google.com")!,
.init(rawValue: "Foo"): 42
Expand Down
23 changes: 17 additions & 6 deletions SwiftyAttributesTests/NSAttributedString_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,26 @@ class NSAttributedString_Tests: XCTestCase {
}

func testAttributeAtLocation_strikethroughStyle() {
let str = "Hello".withStrikethroughStyle(.styleDouble)
let style: NSUnderlineStyle
#if swift(>=4.2)
style = .double
#else
style = .styleDouble
#endif

let str = "Hello".withStrikethroughStyle(style)
let subject = str.swiftyAttribute(.strikethroughStyle, at: 0, effectiveRange: nil)!.value as! StrikethroughStyle
#if swift(>=4.0)
let expected = str.attribute(.strikethroughStyle, at: 0, effectiveRange: nil) as! Int
#else
let expected = str.attribute(NSStrikethroughStyleAttributeName, at: 0, effectiveRange: nil) as! Int
#endif
XCTAssertEqual(subject.rawValue, expected)
XCTAssertEqual(subject, .styleDouble)
#if swift(>=4.2)
XCTAssertEqual(subject, .double)
#else
XCTAssertEqual(subject, .styleDouble)
#endif
}

func testAttributeAtLocation_textColor() {
Expand Down Expand Up @@ -441,7 +452,7 @@ class NSAttributedString_Tests: XCTestCase {
func testFontAttributes_onlyFontAttributes() {
let subject = "Hello".withFont(.systemFont(ofSize: 19)).fontAttributes(in: 0 ..< 3).foundationAttributes
#if swift(>=4.0)
let attributeName = NSAttributedStringKey.font
let attributeName = AttributeName.font
#else
let attributeName = NSFontAttributeName
#endif
Expand All @@ -454,8 +465,8 @@ class NSAttributedString_Tests: XCTestCase {
let url = URL(string: "www.google.com")!
let subject = "Hello".withAttributes([.font(.systemFont(ofSize: 19)), .link(url)]).fontAttributes(in: 0 ..< 3).foundationAttributes
#if swift(>=4.0)
let fontAttributeName = NSAttributedStringKey.font
let linkAttributeName = NSAttributedStringKey.link
let fontAttributeName = AttributeName.font
let linkAttributeName = AttributeName.link
#else
let fontAttributeName = NSFontAttributeName
let linkAttributeName = NSLinkAttributeName
Expand All @@ -472,7 +483,7 @@ class NSAttributedString_Tests: XCTestCase {
style.alignment = .center
let subject = "Hello".withParagraphStyle(style).rulerAttributes(in: 1 ..< 3).foundationAttributes
#if swift(>=4.0)
let attributeName = NSAttributedStringKey.paragraphStyle
let attributeName = AttributeName.paragraphStyle
#else
let attributeName = NSParagraphStyleAttributeName
#endif
Expand Down
12 changes: 6 additions & 6 deletions SwiftyAttributesTests/NSMutableAttributedString_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
subject.addAttributes([.link(URL(string: "https://google.com")!)], range: 1 ..< 3)
XCTAssertNotEqual(subject, expected)
#if swift(>=4.0)
let linkAttributeName = NSAttributedStringKey.link
let linkAttributeName = AttributeName.link
#else
let linkAttributeName = NSLinkAttributeName
#endif
Expand All @@ -33,7 +33,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
subject.addAttributes([.font(.boldSystemFont(ofSize: 17))], range: NSRange(location: 0, length: 1))
XCTAssertNotEqual(subject, expected)
#if swift(>=4.0)
let fontAttributeName = NSAttributedStringKey.font
let fontAttributeName = AttributeName.font
#else
let fontAttributeName = NSFontAttributeName
#endif
Expand All @@ -48,7 +48,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
subject.setAttributes([.backgroundColor(.orange)], range: 0 ..< 3)
XCTAssertNotEqual(subject, expected)
#if swift(>=4.0)
let attributeName = NSAttributedStringKey.backgroundColor
let attributeName = AttributeName.backgroundColor
#else
let attributeName = NSBackgroundColorAttributeName
#endif
Expand All @@ -63,7 +63,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
subject.setAttributes([.backgroundColor(.orange)], range: NSRange(location: 2, length: 2))
XCTAssertNotEqual(subject, expected)
#if swift(>=4.0)
let attributeName = NSAttributedStringKey.backgroundColor
let attributeName = AttributeName.backgroundColor
#else
let attributeName = NSBackgroundColorAttributeName
#endif
Expand All @@ -88,7 +88,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
subject.replaceCharacters(in: 0 ..< 2, with: "Chi".withBackgroundColor(.magenta))
XCTAssertNotEqual(subject, expected)
#if swift(>=4.0)
let attributeName = NSAttributedStringKey.backgroundColor
let attributeName = AttributeName.backgroundColor
#else
let attributeName = NSBackgroundColorAttributeName
#endif
Expand All @@ -113,7 +113,7 @@ class NSMutableAttributedString_Tests: XCTestCase {
subject.removeAttribute(.baselineOffset, range: 1 ..< 4)
XCTAssertNotEqual(subject, expected)
#if swift(>=4.0)
let attributeName = NSAttributedStringKey.baselineOffset
let attributeName = AttributeName.baselineOffset
#else
let attributeName = NSBaselineOffsetAttributeName
#endif
Expand Down
4 changes: 2 additions & 2 deletions SwiftyAttributesTests/Operators_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Operators_Tests: XCTestCase {
let rhs = "World".withTextColor(.magenta).withBackgroundColor(.orange).withFont(.boldSystemFont(ofSize: 24))
let newString = lhs + rhs
#if swift(>=4.0)
let leftAttributes: [NSAttributedStringKey: NSObject] = [.font: Font.systemFont(ofSize: 19)]
let leftAttributes: [AttributeName: NSObject] = [.font: Font.systemFont(ofSize: 19)]
XCTAssertEqual(newString.attributes(at: 0, effectiveRange: nil) as NSDictionary, leftAttributes as NSDictionary)
let rightAttributes: [NSAttributedStringKey: NSObject] = [
let rightAttributes: [AttributeName: NSObject] = [
.foregroundColor: Color.magenta,
.backgroundColor: Color.orange,
.font: Font.boldSystemFont(ofSize: 24)
Expand Down
Loading

0 comments on commit f1cacc8

Please sign in to comment.