Skip to content

Commit

Permalink
Merge pull request #56 from kapoorsahil/swift4.2
Browse files Browse the repository at this point in the history
Swift 4.2 migration
  • Loading branch information
Nicholas Maccharoli authored Sep 25, 2018
2 parents 4520e70 + f8eb38f commit c9b75a2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
16 changes: 6 additions & 10 deletions Attributed.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@
TargetAttributes = {
AAEE626C1DF8F3690079C70C = {
CreatedOnToolsVersion = 8.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1000;
ProvisioningStyle = Automatic;
};
AAEE62751DF8F3690079C70C = {
CreatedOnToolsVersion = 8.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1000;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -413,8 +413,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -433,8 +432,7 @@
PRODUCT_BUNDLE_IDENTIFIER = attributed.Attributed;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand All @@ -446,8 +444,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = attributed.AttributedTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -459,8 +456,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = attributed.AttributedTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down
84 changes: 42 additions & 42 deletions Attributed/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Foundation

public struct Attributes {

public let dictionary: [NSAttributedStringKey: Any]
public let dictionary: [NSAttributedString.Key: Any]

public init() {
dictionary = [:]
Expand All @@ -36,62 +36,62 @@ public struct Attributes {
self = attributesBlock(Attributes())
}

internal init(dictionary: [NSAttributedStringKey: Any]) {
internal init(dictionary: [NSAttributedString.Key: Any]) {
self.dictionary = dictionary
}

public func font(_ font: UIFont) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.font: font])
return self + Attributes(dictionary: [NSAttributedString.Key.font: font])
}

public func kerning(_ kerning: Double) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.kern: NSNumber(floatLiteral: kerning)])
return self + Attributes(dictionary: [NSAttributedString.Key.kern: NSNumber(floatLiteral: kerning)])
}

public func strikeThroughStyle(_ strikeThroughStyle: NSUnderlineStyle) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.strikethroughStyle: strikeThroughStyle.rawValue,
NSAttributedStringKey.baselineOffset : NSNumber(floatLiteral: 1.5),
return self + Attributes(dictionary: [NSAttributedString.Key.strikethroughStyle: strikeThroughStyle.rawValue,
NSAttributedString.Key.baselineOffset : NSNumber(floatLiteral: 1.5),
])
}

public func underlineStyle(_ underlineStyle: NSUnderlineStyle) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.underlineStyle: underlineStyle.rawValue])
return self + Attributes(dictionary: [NSAttributedString.Key.underlineStyle: underlineStyle.rawValue])
}

public func strokeColor(_ strokeColor: UIColor) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.strokeColor: strokeColor])
return self + Attributes(dictionary: [NSAttributedString.Key.strokeColor: strokeColor])
}

public func strokeWidth(_ strokewidth: Double) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.strokeWidth: NSNumber(floatLiteral: strokewidth)])
return self + Attributes(dictionary: [NSAttributedString.Key.strokeWidth: NSNumber(floatLiteral: strokewidth)])
}

public func foreground(color: UIColor) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.foregroundColor: color])
return self + Attributes(dictionary: [NSAttributedString.Key.foregroundColor: color])
}

public func background(color: UIColor) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.backgroundColor: color])
return self + Attributes(dictionary: [NSAttributedString.Key.backgroundColor: color])
}

public func paragraphStyle(_ paragraphStyle: NSParagraphStyle) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func shadow(_ shadow: NSShadow) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.shadow: shadow])
return self + Attributes(dictionary: [NSAttributedString.Key.shadow: shadow])
}

public func obliqueness(_ value: CGFloat) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.obliqueness: value])
return self + Attributes(dictionary: [NSAttributedString.Key.obliqueness: value])
}

public func link(_ link: String) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.link: link])
return self + Attributes(dictionary: [NSAttributedString.Key.link: link])
}

public func baselineOffset(_ offset: NSNumber) -> Attributes {
return self + Attributes(dictionary: [NSAttributedStringKey.baselineOffset: offset])
return self + Attributes(dictionary: [NSAttributedString.Key.baselineOffset: offset])
}
}

Expand All @@ -100,84 +100,84 @@ public struct Attributes {
extension Attributes {

public func lineSpacing(_ lineSpacing: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.lineSpacing = lineSpacing
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func paragraphSpacing(_ paragraphSpacing: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.paragraphSpacing = paragraphSpacing
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func alignment(_ alignment: NSTextAlignment) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.alignment = alignment
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func firstLineHeadIndent(_ firstLineHeadIndent: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.firstLineHeadIndent = firstLineHeadIndent
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func headIndent(_ headIndent: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.headIndent = headIndent
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func tailIndent(_ tailIndent: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.tailIndent = tailIndent
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func lineBreakMode(_ lineBreakMode: NSLineBreakMode) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.lineBreakMode = lineBreakMode
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func minimumLineHeight(_ minimumLineHeight: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.minimumLineHeight = minimumLineHeight
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func maximumLineHeight(_ maximumLineHeight: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.maximumLineHeight = maximumLineHeight
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func uniformLineHeight(_ uniformLineHeight: CGFloat) -> Attributes {
return maximumLineHeight(uniformLineHeight).minimumLineHeight(uniformLineHeight)
}

public func baseWritingDirection(_ baseWritingDirection: NSWritingDirection) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.baseWritingDirection = baseWritingDirection
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func lineHeightMultiple(_ lineHeightMultiple: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.lineHeightMultiple = lineHeightMultiple
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func paragraphSpacingBefore(_ paragraphSpacingBefore: CGFloat) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.paragraphSpacingBefore = paragraphSpacingBefore
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}

public func hyphenationFactor(_ hyphenationFactor: Float) -> Attributes {
let paragraphStyle = (dictionary[NSAttributedStringKey.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
let paragraphStyle = (dictionary[NSAttributedString.Key.paragraphStyle] ?? NSMutableParagraphStyle.default.mutableCopy()) as! NSMutableParagraphStyle
paragraphStyle.hyphenationFactor = hyphenationFactor
return self + Attributes(dictionary: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return self + Attributes(dictionary: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
}
}
6 changes: 3 additions & 3 deletions Attributed/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public func + (lhs: Attributes, rhs: Attributes) -> Attributes {
}

let combinedParagraphStyle: NSParagraphStyle?
let lhsParagraphStyle = lhs.dictionary[NSAttributedStringKey.paragraphStyle] as? NSParagraphStyle
let rhsParagraphStyle = rhs.dictionary[NSAttributedStringKey.paragraphStyle] as? NSParagraphStyle
let lhsParagraphStyle = lhs.dictionary[NSAttributedString.Key.paragraphStyle] as? NSParagraphStyle
let rhsParagraphStyle = rhs.dictionary[NSAttributedString.Key.paragraphStyle] as? NSParagraphStyle

if let lhsParagraphStyle = lhsParagraphStyle, let rhsParagraphStyle = rhsParagraphStyle {
combinedParagraphStyle = lhsParagraphStyle + rhsParagraphStyle
Expand All @@ -124,7 +124,7 @@ public func + (lhs: Attributes, rhs: Attributes) -> Attributes {
}

if let paragraphStyle = combinedParagraphStyle {
combined[NSAttributedStringKey.paragraphStyle] = paragraphStyle
combined[NSAttributedString.Key.paragraphStyle] = paragraphStyle
}

return Attributes(dictionary: combined)
Expand Down
4 changes: 2 additions & 2 deletions AttributedTests/AttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import XCTest

class AttributesTests: XCTestCase {
func testFont() {
let expected: [String: Any] = [NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 12.0)]
let expected: [String: Any] = [NSAttributedString.Key.font.rawValue: UIFont.systemFont(ofSize: 12.0)]
let attributed = Attributes().font(.systemFont(ofSize: 12.0))
XCTAssert(attributed.dictionary.keys.first!.rawValue == expected.keys.first!, "Font is broken")
}

func testBaselineOffset() {
let expected: [String: Any] = [NSAttributedStringKey.baselineOffset.rawValue: NSNumber(value: 1)]
let expected: [String: Any] = [NSAttributedString.Key.baselineOffset.rawValue: NSNumber(value: 1)]
let attributed = Attributes().baselineOffset(1)
XCTAssert(attributed.dictionary.keys.first!.rawValue == expected.keys.first!, "NSBaselineOffsetAttributeName functionality is broken")
}
Expand Down

0 comments on commit c9b75a2

Please sign in to comment.