From 548e4465f7bb78a461006028f7acb8600d26b293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=CC=81bio=20Almeida?= Date: Fri, 24 Nov 2017 00:30:40 +0000 Subject: [PATCH 1/3] Migrated to Swift 4. Updated generic method interfaced to add attributes to use the new NSAttributedStringKey type --- .swift-version | 2 +- .travis.yml | 4 ++-- .../project.pbxproj | 16 ++++++------- .../LabelConfigurator-Example.xcscheme | 2 +- .../xcshareddata/WorkspaceSettings.xcsettings | 5 ++++ .../LabelConfigurator/ViewController.swift | 2 +- Example/Podfile.lock | 4 ++-- Example/Pods/Manifest.lock | 4 ++-- LabelConfigurator.podspec | 4 ++-- .../Classes/NSAttributedStringBuilder.swift | 24 +++++++++---------- .../Classes/UILabelBuilder.swift | 4 ++-- README.md | 4 ++-- 12 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 Example/LabelConfigurator.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/.swift-version b/.swift-version index 9f55b2c..5186d07 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -3.0 +4.0 diff --git a/.travis.yml b/.travis.yml index 3fe1970..04aaa7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ # * http://www.objc.io/issue-6/travis-ci.html # * https://github.com/supermarin/xcpretty#usage -osx_image: xcode8.3 +osx_image: xcode9 language: swift cache: cocoapods podfile: Example/Podfile @@ -10,5 +10,5 @@ before_install: - gem install cocoapods # Since Travis is not always on latest version - pod install --project-directory=Example script: -- set -o pipefail && xcodebuild test -workspace Example/LabelConfigurator.xcworkspace -scheme LabelConfigurator-Example -destination 'platform=iOS Simulator,name=iPhone 6s,OS=10.3.1' -sdk iphonesimulator10.3 ONLY_ACTIVE_ARCH=NO | xcpretty +- set -o pipefail && xcodebuild test -workspace Example/LabelConfigurator.xcworkspace -scheme LabelConfigurator-Example -destination 'platform=iOS Simulator,name=iPhone 7,OS=11.0' -sdk iphonesimulator11.0 ONLY_ACTIVE_ARCH=NO | xcpretty - pod lib lint diff --git a/Example/LabelConfigurator.xcodeproj/project.pbxproj b/Example/LabelConfigurator.xcodeproj/project.pbxproj index 6f61ac5..a038f21 100644 --- a/Example/LabelConfigurator.xcodeproj/project.pbxproj +++ b/Example/LabelConfigurator.xcodeproj/project.pbxproj @@ -215,11 +215,11 @@ TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; - LastSwiftMigration = 0900; + LastSwiftMigration = 0910; }; 607FACE41AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; - LastSwiftMigration = 0900; + LastSwiftMigration = 0910; TestTargetID = 607FACCF1AFB9204008FA782; }; }; @@ -466,7 +466,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -512,7 +512,7 @@ MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; VALIDATE_PRODUCT = YES; }; name = Release; @@ -529,7 +529,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -545,7 +545,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -566,7 +566,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -583,7 +583,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/Example/LabelConfigurator.xcodeproj/xcshareddata/xcschemes/LabelConfigurator-Example.xcscheme b/Example/LabelConfigurator.xcodeproj/xcshareddata/xcschemes/LabelConfigurator-Example.xcscheme index 7bde18b..868dc22 100644 --- a/Example/LabelConfigurator.xcodeproj/xcshareddata/xcschemes/LabelConfigurator-Example.xcscheme +++ b/Example/LabelConfigurator.xcodeproj/xcshareddata/xcschemes/LabelConfigurator-Example.xcscheme @@ -1,6 +1,6 @@ + + + + diff --git a/Example/LabelConfigurator/ViewController.swift b/Example/LabelConfigurator/ViewController.swift index 6d72ead..3260b25 100644 --- a/Example/LabelConfigurator/ViewController.swift +++ b/Example/LabelConfigurator/ViewController.swift @@ -67,7 +67,7 @@ class ViewController: UIViewController { .set(textColor: .black) .set(textColor: .red, onSubstring: newPrice) .set(textColor: .lightGray, onSubstring: oldPrice) - .set(attribute: NSStrikethroughStyleAttributeName, value: NSNumber(value: 1), onSubstring: oldPrice) + .set(attribute: NSAttributedStringKey.strikethroughStyle, value: 1, onSubstring: oldPrice) .configure() self.stackView.addArrangedSubview(label4) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index ef03112..72a35bf 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - LabelConfigurator (0.1.2) + - LabelConfigurator (0.3.0) DEPENDENCIES: - LabelConfigurator (from `../`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: ../ SPEC CHECKSUMS: - LabelConfigurator: 0f5235e9a35927dc55233ce511031c4f6f1dbc58 + LabelConfigurator: 39ff7b3dcdb2a78945a68ab34650a8f6ca833965 PODFILE CHECKSUM: 08e5c5bb7cf9dbc2a1d76bd7b3cf459830a63839 diff --git a/Example/Pods/Manifest.lock b/Example/Pods/Manifest.lock index ef03112..72a35bf 100644 --- a/Example/Pods/Manifest.lock +++ b/Example/Pods/Manifest.lock @@ -1,5 +1,5 @@ PODS: - - LabelConfigurator (0.1.2) + - LabelConfigurator (0.3.0) DEPENDENCIES: - LabelConfigurator (from `../`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: ../ SPEC CHECKSUMS: - LabelConfigurator: 0f5235e9a35927dc55233ce511031c4f6f1dbc58 + LabelConfigurator: 39ff7b3dcdb2a78945a68ab34650a8f6ca833965 PODFILE CHECKSUM: 08e5c5bb7cf9dbc2a1d76bd7b3cf459830a63839 diff --git a/LabelConfigurator.podspec b/LabelConfigurator.podspec index 56794c4..f74a8c4 100644 --- a/LabelConfigurator.podspec +++ b/LabelConfigurator.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'LabelConfigurator' - s.version = '0.2.0' + s.version = '0.3.0' s.summary = 'The most simple way to configure your UILabels in one go' s.description = <<-DESC The most simple way to configure your UILabels in one go. @@ -16,7 +16,7 @@ It also helps on the hard task of adding different style to substrings. s.social_media_url = 'https://twitter.com/fabioacalmeida' s.ios.deployment_target = '9.0' - s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.0' } + s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' } s.source_files = 'LabelConfigurator/Classes/**/*' end diff --git a/LabelConfigurator/Classes/NSAttributedStringBuilder.swift b/LabelConfigurator/Classes/NSAttributedStringBuilder.swift index 95a9ce2..2491f69 100644 --- a/LabelConfigurator/Classes/NSAttributedStringBuilder.swift +++ b/LabelConfigurator/Classes/NSAttributedStringBuilder.swift @@ -11,15 +11,15 @@ import Foundation private struct SubstringAttribute { let substring: String - let attribute: String - let value: AnyObject + let attribute: NSAttributedStringKey + let value: Any } private struct RangeAttribute { let range: NSRange - let attribute: String - let value: AnyObject + let attribute: NSAttributedStringKey + let value: Any } open class NSAttributedStringBuilder: NSObject { @@ -90,19 +90,19 @@ open class NSAttributedStringBuilder: NSObject { @objc(setFont:onSubstring:) open func set(font: UIFont?, onSubstring substring: String?) -> NSAttributedStringBuilder { - return self.set(attribute: NSFontAttributeName, value: font, onSubstring: substring) + return self.set(attribute: NSAttributedStringKey.font, value: font, onSubstring: substring) } @discardableResult @objc(setTextColor:onSubstring:) open func set(textColor color: UIColor?, onSubstring substring: String?) -> NSAttributedStringBuilder { - return self.set(attribute: NSForegroundColorAttributeName, value: color, onSubstring: substring) + return self.set(attribute: NSAttributedStringKey.foregroundColor, value: color, onSubstring: substring) } @discardableResult @objc(setAttribute:value:onSubstring:) - open func set(attribute: String?, value: AnyObject?, onSubstring substring: String?) -> NSAttributedStringBuilder { + open func set(attribute: NSAttributedStringKey?, value: Any?, onSubstring substring: String?) -> NSAttributedStringBuilder { if let attribute = attribute, let value = value, let substring = substring { @@ -121,7 +121,7 @@ open class NSAttributedStringBuilder: NSObject { @discardableResult @objc(setAttribute:value:onRange:) - open func set(attribute: String?, value: AnyObject?, onRange range: NSRange) -> NSAttributedStringBuilder { + open func set(attribute: NSAttributedStringKey?, value: Any?, onRange range: NSRange) -> NSAttributedStringBuilder { if let attribute = attribute, let value = value { @@ -151,17 +151,17 @@ private extension NSAttributedStringBuilder { if let textColor = self.textColor { - attributedString.addAttribute(NSForegroundColorAttributeName, value: textColor, range: fullRange) + attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: textColor, range: fullRange) } if let textFont = self.textFont { - attributedString.addAttribute(NSFontAttributeName, value: textFont, range: fullRange) + attributedString.addAttribute(NSAttributedStringKey.font, value: textFont, range: fullRange) } if let fontTracking = self.fontTracking { - attributedString.addAttribute(NSKernAttributeName, value: fontTracking, range: fullRange) + attributedString.addAttribute(NSAttributedStringKey.kern, value: fontTracking, range: fullRange) } var paragraphStyle: NSMutableParagraphStyle? @@ -184,7 +184,7 @@ private extension NSAttributedStringBuilder { if let paragraphStyle = paragraphStyle { - attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: fullRange) + attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: fullRange) } if let substringAttributes = self.substringAttributes { diff --git a/LabelConfigurator/Classes/UILabelBuilder.swift b/LabelConfigurator/Classes/UILabelBuilder.swift index 7f0eff4..29109e6 100644 --- a/LabelConfigurator/Classes/UILabelBuilder.swift +++ b/LabelConfigurator/Classes/UILabelBuilder.swift @@ -117,7 +117,7 @@ open class UILabelBuilder: NSObject { @discardableResult @objc(setAttribute:value:onSubstring:) - open func set(attribute: String, value: AnyObject, onSubstring substring: String) -> UILabelBuilder { + open func set(attribute: NSAttributedStringKey, value: Any, onSubstring substring: String) -> UILabelBuilder { self.attributedStringBuilder.set(attribute: attribute, value: value, onSubstring: substring) self.shouldSetAttributedText = true @@ -126,7 +126,7 @@ open class UILabelBuilder: NSObject { @discardableResult @objc(setAttribute:value:onRange:) - open func set(attribute: String, value: AnyObject, onRange range: NSRange) -> UILabelBuilder { + open func set(attribute: NSAttributedStringKey, value: Any, onRange range: NSRange) -> UILabelBuilder { self.attributedStringBuilder.set(attribute: attribute, value: value, onRange: range) self.shouldSetAttributedText = true diff --git a/README.md b/README.md index 5625a4e..d9a3229 100644 --- a/README.md +++ b/README.md @@ -107,8 +107,8 @@ The most common customisations done on `UILabels` are covered with helper method However, not all the possibilities are covered (and we want to keep it that way) on `UILabel` attributes and also `NSAttributedString`. The good this is that you can add your custom attributes you wanted to add to your `NSAttributedString` using two convenience methods to do so: -- `func set(attribute: String, value: AnyObject, onSubstring substring: String)` -- `func set(attribute: String, value: AnyObject, onRange range: NSRange)` +- `func set(attribute: NSAttributedStringKey, value: Any, onSubstring substring: String)` +- `func set(attribute: NSAttributedStringKey, value: Any, onRange range: NSRange)` For example, if you want to add a strikethrough attribute to a substring on your `UILabel`, you can do as the following From 866adc8d18daf3796134e55ba20ea6b57523a425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=CC=81bio=20Almeida?= Date: Fri, 24 Nov 2017 00:31:29 +0000 Subject: [PATCH 2/3] Updated versions on README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d9a3229..d177a51 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ platform :ios, '9.0' use_frameworks! target 'TargetName' do - pod 'LabelConfigurator', '~> 0.2.0' + pod 'LabelConfigurator', '~> 0.3.0' end ``` @@ -46,7 +46,7 @@ After specifying the new dependency on the Podfile, just run `pod install` to ma To integrate `LabelConfigurator` into your Xcode project using Carthage, specify it in your `Cartfile`: ```ogdl -github "fabioameida/LabelConfigurator" ~> 0.2.0 +github "fabioameida/LabelConfigurator" ~> 0.3.0 ``` Run `carthage update` to build the framework and drag the built `LabelConfigurator.framework` into your Xcode project. From 821b74c29dd2ec0a2078f30802f42debab76584c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fa=CC=81bio=20Almeida?= Date: Fri, 24 Nov 2017 00:40:20 +0000 Subject: [PATCH 3/3] Updated example on README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d177a51..39407c2 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ self.myLabel.setLabelText("New price \(oldPrice) \(newPrice)") .set(textColor: .black) .set(textColor: .red, onSubstring: newPrice) .set(textColor: .lightGray, onSubstring: oldPrice) - .set(attribute: NSStrikethroughStyleAttributeName, value: NSNumber(value: 1), onSubstring: oldPrice) + .set(attribute: NSAttributedStringKey.strikethroughStyle, value: 1, onSubstring: oldPrice) .configure() ```