Skip to content

Commit

Permalink
Merge pull request #2 from fabioalmeida/dev
Browse files Browse the repository at this point in the history
Swift 4 migration
  • Loading branch information
fabioalmeida authored Nov 24, 2017
2 parents 90d29ab + 821b74c commit 82d721d
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
4.0
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# * 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
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
16 changes: 8 additions & 8 deletions Example/LabelConfigurator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
Expand Down Expand Up @@ -466,7 +466,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
2 changes: 1 addition & 1 deletion Example/LabelConfigurator/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- LabelConfigurator (0.1.2)
- LabelConfigurator (0.3.0)

DEPENDENCIES:
- LabelConfigurator (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: ../

SPEC CHECKSUMS:
LabelConfigurator: 0f5235e9a35927dc55233ce511031c4f6f1dbc58
LabelConfigurator: 39ff7b3dcdb2a78945a68ab34650a8f6ca833965

PODFILE CHECKSUM: 08e5c5bb7cf9dbc2a1d76bd7b3cf459830a63839

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions LabelConfigurator.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
24 changes: 12 additions & 12 deletions LabelConfigurator/Classes/NSAttributedStringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {

Expand All @@ -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 {

Expand Down Expand Up @@ -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?
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions LabelConfigurator/Classes/UILabelBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ platform :ios, '9.0'
use_frameworks!

target 'TargetName' do
pod 'LabelConfigurator', '~> 0.2.0'
pod 'LabelConfigurator', '~> 0.3.0'
end
```

Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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()
```

Expand Down

0 comments on commit 82d721d

Please sign in to comment.