Skip to content

Commit

Permalink
Merge pull request #202 from optonaut/swift-4
Browse files Browse the repository at this point in the history
Swift 4
  • Loading branch information
polqf authored Sep 15, 2017
2 parents 5c030f3 + e1977df commit 3f869d1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: objective-c
osx_image: xcode8
osx_image: xcode9
before_install:
- gem install scan
- scan --version
install: true
script:
- scan -p "ActiveLabel.xcodeproj" -s "ActiveLabel"
- scan -p "ActiveLabel.xcodeproj" -s "ActiveLabel"
35 changes: 27 additions & 8 deletions ActiveLabel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,20 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = Optonaut;
TargetAttributes = {
8F0249A11B9989B1005D8035 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
8F0249AB1B9989B1005D8035 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0800;
};
8F0249BF1B998A66005D8035 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0900;
};
};
};
Expand Down Expand Up @@ -371,14 +372,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -407,6 +414,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -421,14 +429,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand All @@ -450,6 +464,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -474,7 +489,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -494,7 +510,8 @@
PRODUCT_BUNDLE_IDENTIFIER = optonaut.ActiveLabel;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -505,7 +522,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = optonaut.ActiveLabelTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -516,7 +533,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = optonaut.ActiveLabelTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -530,7 +547,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = optonaut.ActiveLabelDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -544,7 +562,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = optonaut.ActiveLabelDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
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 = "0800"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
30 changes: 15 additions & 15 deletions ActiveLabel/ActiveLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol ActiveLabelDelegate: class {
func didSelect(_ text: String, type: ActiveType)
}

public typealias ConfigureLinkAttribute = (ActiveType, [String : Any], Bool) -> ([String : Any])
public typealias ConfigureLinkAttribute = (ActiveType, [NSAttributedStringKey : Any], Bool) -> ([NSAttributedStringKey : Any])
typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveType)

@IBDesignable open class ActiveLabel: UILabel {
Expand Down Expand Up @@ -60,7 +60,7 @@ typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveTy
@IBInspectable public var highlightFontName: String? = nil {
didSet { updateTextStorage(parseText: false) }
}
@IBInspectable public var highlightFontSize: CGFloat? = nil {
public var highlightFontSize: CGFloat? = nil {
didSet { updateTextStorage(parseText: false) }
}

Expand Down Expand Up @@ -306,23 +306,23 @@ typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveTy
var range = NSRange(location: 0, length: 0)
var attributes = mutAttrString.attributes(at: 0, effectiveRange: &range)

attributes[NSFontAttributeName] = font!
attributes[NSForegroundColorAttributeName] = textColor
attributes[NSAttributedStringKey.font] = font!
attributes[NSAttributedStringKey.foregroundColor] = textColor
mutAttrString.addAttributes(attributes, range: range)

attributes[NSForegroundColorAttributeName] = mentionColor
attributes[NSAttributedStringKey.foregroundColor] = mentionColor

for (type, elements) in activeElements {

switch type {
case .mention: attributes[NSForegroundColorAttributeName] = mentionColor
case .hashtag: attributes[NSForegroundColorAttributeName] = hashtagColor
case .url: attributes[NSForegroundColorAttributeName] = URLColor
case .custom: attributes[NSForegroundColorAttributeName] = customColor[type] ?? defaultCustomColor
case .mention: attributes[NSAttributedStringKey.foregroundColor] = mentionColor
case .hashtag: attributes[NSAttributedStringKey.foregroundColor] = hashtagColor
case .url: attributes[NSAttributedStringKey.foregroundColor] = URLColor
case .custom: attributes[NSAttributedStringKey.foregroundColor] = customColor[type] ?? defaultCustomColor
}

if let highlightFont = hightlightFont {
attributes[NSFontAttributeName] = highlightFont
attributes[NSAttributedStringKey.font] = highlightFont
}

if let configureLinkAttribute = configureLinkAttribute {
Expand Down Expand Up @@ -373,12 +373,12 @@ typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveTy
var range = NSRange(location: 0, length: 0)
var attributes = mutAttrString.attributes(at: 0, effectiveRange: &range)

let paragraphStyle = attributes[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
let paragraphStyle = attributes[NSAttributedStringKey.paragraphStyle] as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
paragraphStyle.alignment = textAlignment
paragraphStyle.lineSpacing = lineSpacing
paragraphStyle.minimumLineHeight = minimumLineHeight > 0 ? minimumLineHeight: self.font.pointSize * 1.14
attributes[NSParagraphStyleAttributeName] = paragraphStyle
attributes[NSAttributedStringKey.paragraphStyle] = paragraphStyle
mutAttrString.setAttributes(attributes, range: range)

return mutAttrString
Expand All @@ -402,7 +402,7 @@ typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveTy
let possibleSelectedColor = customSelectedColor[selectedElement.type] ?? customColor[selectedElement.type]
selectedColor = possibleSelectedColor ?? defaultCustomColor
}
attributes[NSForegroundColorAttributeName] = selectedColor
attributes[NSAttributedStringKey.foregroundColor] = selectedColor
} else {
let unselectedColor: UIColor
switch type {
Expand All @@ -411,11 +411,11 @@ typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveTy
case .url: unselectedColor = URLColor
case .custom: unselectedColor = customColor[selectedElement.type] ?? defaultCustomColor
}
attributes[NSForegroundColorAttributeName] = unselectedColor
attributes[NSAttributedStringKey.foregroundColor] = unselectedColor
}

if let highlightFont = hightlightFont {
attributes[NSFontAttributeName] = highlightFont
attributes[NSAttributedStringKey.font] = highlightFont
}

if let configureLinkAttribute = configureLinkAttribute {
Expand Down
2 changes: 1 addition & 1 deletion ActiveLabel/StringTrimExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import Foundation
extension String {

func trim(to maximumCharacters: Int) -> String {
return substring(to: index(startIndex, offsetBy: maximumCharacters)) + "..."
return "\(self[..<index(startIndex, offsetBy: maximumCharacters)])" + "..."
}
}
2 changes: 1 addition & 1 deletion ActiveLabelDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ViewController: UIViewController {
var atts = attributes
switch type {
case customType3:
atts[NSFontAttributeName] = isSelected ? UIFont.boldSystemFont(ofSize: 16) : UIFont.boldSystemFont(ofSize: 14)
atts[NSAttributedStringKey.font] = isSelected ? UIFont.boldSystemFont(ofSize: 16) : UIFont.boldSystemFont(ofSize: 14)
default: ()
}

Expand Down
4 changes: 2 additions & 2 deletions ActiveLabelTests/ActiveTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class ActiveTypeTests: XCTestCase {
label.configureLinkAttribute = { type, attributes, isSelected in
var atts = attributes
if case newType = type {
atts[NSFontAttributeName] = UIFont.boldSystemFont(ofSize: 14)
atts[NSAttributedStringKey.font] = UIFont.boldSystemFont(ofSize: 14)
}

return atts
Expand All @@ -248,7 +248,7 @@ class ActiveTypeTests: XCTestCase {
// Enumber after attributes and find our font
var foundCustomAttributedStyling = false
areText.enumerateAttributes(in: NSRange(location: 0, length: areText.length), options: [.longestEffectiveRangeNotRequired], using: { (attributes, range, stop) in
foundCustomAttributedStyling = attributes[NSFontAttributeName] as? UIFont == UIFont.boldSystemFont(ofSize: 14)
foundCustomAttributedStyling = attributes[NSAttributedStringKey.font] as? UIFont == UIFont.boldSystemFont(ofSize: 14)
})

XCTAssertTrue(foundCustomAttributedStyling)
Expand Down

0 comments on commit 3f869d1

Please sign in to comment.