From 1bbfd203d6ca4ec64717a00c18760acade794acf Mon Sep 17 00:00:00 2001 From: Felipe Espinoza Date: Thu, 9 Nov 2017 14:11:41 +0100 Subject: [PATCH 1/6] Add swift formatting for readme --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d0a5044..9fdb799 100644 --- a/README.md +++ b/README.md @@ -24,17 +24,17 @@ pod 'LinkLabel' ## Usage Create a label using `LinkLabel`, instead of UILabel. -``` +```swift let myLabel = LinkLabel() ``` Setup attributed text, including `NSLinkAttributeName`, as normal. -``` +```swift let text = "This is some text, which includes a link." let fullRange = NSMakeRange(0, (text as NSString).length) let linkRange = (text as NSString).rangeOfString("includes a link") - + let attributedString = NSMutableAttributedString(string: text) attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: fullRange) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: fullRange) @@ -43,7 +43,7 @@ attributedString.addAttribute(NSLinkAttributeName, value: NSURL(string: "https:/ If you wish to customise the link appearance: -``` +```swift let linkTextAttributes = [ NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue), NSForegroundColorAttributeName: UIColor.greenColor() @@ -60,13 +60,13 @@ label.highlightedLinkTextAttributes = highlightedLinkTextAttributes To make it easier to respond to link taps, I’ve added in an interaction delegate. Adopt `LinkLabelInteractionDelegate`, and then implement the delegate function: -``` +```swift label.interactionDelegate = self ``` -``` +```swift //MARK: LinkLabelInteractionDelegate - + func linkLabelDidSelectLink(linkLabel linkLabel: LinkLabel, url: NSURL) { print("did select link: \(url)") } From 156027913e4505ca48e0e74a5bfd6484d0d8a426 Mon Sep 17 00:00:00 2001 From: Felipe Espinoza Date: Thu, 9 Nov 2017 14:41:36 +0100 Subject: [PATCH 2/6] Update project to swift 4 compiled with xcode 9.1 --- .swift-version | 1 + LinkLabelDemo.xcodeproj/project.pbxproj | 27 +++++++++++--- LinkLabelDemo/ViewController.swift | 27 ++++++-------- Source/LinkLabel.swift | 36 ++++++++++++------- ...bleAttributedString+RemoveAttributes.swift | 3 +- 5 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 .swift-version diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..5186d07 --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +4.0 diff --git a/LinkLabelDemo.xcodeproj/project.pbxproj b/LinkLabelDemo.xcodeproj/project.pbxproj index 0b1e5ca..6de3373 100644 --- a/LinkLabelDemo.xcodeproj/project.pbxproj +++ b/LinkLabelDemo.xcodeproj/project.pbxproj @@ -111,12 +111,13 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0710; - LastUpgradeCheck = 0800; + LastUpgradeCheck = 0910; ORGANIZATIONNAME = "Project Dent"; TargetAttributes = { 4940B3071C04BF3C000524A0 = { CreatedOnToolsVersion = 7.1; - LastSwiftMigration = 0800; + DevelopmentTeam = 2Z7R9FQBNN; + LastSwiftMigration = 0910; }; }; }; @@ -195,14 +196,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; @@ -230,6 +237,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -242,14 +250,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; @@ -270,6 +284,7 @@ MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -280,6 +295,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; + DEVELOPMENT_TEAM = 2Z7R9FQBNN; INFOPLIST_FILE = LinkLabelDemo/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -288,7 +304,8 @@ SWIFT_INSTALL_OBJC_HEADER = NO; SWIFT_OBJC_BRIDGING_HEADER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -297,6 +314,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; + DEVELOPMENT_TEAM = 2Z7R9FQBNN; INFOPLIST_FILE = LinkLabelDemo/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -304,7 +322,8 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_INSTALL_OBJC_HEADER = NO; SWIFT_OBJC_BRIDGING_HEADER = ""; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/LinkLabelDemo/ViewController.swift b/LinkLabelDemo/ViewController.swift index 3e4752d..32d8117 100644 --- a/LinkLabelDemo/ViewController.swift +++ b/LinkLabelDemo/ViewController.swift @@ -12,41 +12,36 @@ class ViewController: UIViewController, LinkLabelInteractionDelegate { override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - self.view.backgroundColor = UIColor.white let text = "This is some text, which incidentally includes a link you may find interesting." let fullRange = NSMakeRange(0, (text as NSString).length) let linkRange = (text as NSString).range(of: "includes a link") let attributedString = NSMutableAttributedString(string: text) - attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 15), range: fullRange) - attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: fullRange) - attributedString.addAttribute(NSLinkAttributeName, value: URL(string: "https://google.com")!, range: linkRange) + attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 15), range: fullRange) + attributedString.addAttribute(.foregroundColor, value: UIColor.black, range: fullRange) + attributedString.addAttribute(.link, value: URL(string: "https://google.com")!, range: linkRange) - let linkTextAttributes = [ - NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), - NSForegroundColorAttributeName: UIColor.green + let linkTextAttributes: [NSAttributedStringKey: AnyObject] = [ + NSAttributedStringKey.underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), + NSAttributedStringKey.foregroundColor: UIColor.green ] - let highlightedLinkTextAttributes = [ - NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), - NSForegroundColorAttributeName: UIColor.red + let highlightedLinkTextAttributes: [NSAttributedStringKey: AnyObject] = [ + .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), + .foregroundColor: UIColor.red ] let label = LinkLabel() + label.backgroundColor = UIColor.yellow label.numberOfLines = 0 // Infinite lines label.attributedText = attributedString label.linkTextAttributes = linkTextAttributes label.highlightedLinkTextAttributes = highlightedLinkTextAttributes label.interactionDelegate = self label.frame = CGRect(x: 20, y: 20, width: 280, height: 400) - self.view.addSubview(label) - } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. + self.view.addSubview(label) } //MARK: LinkLabelInteractionDelegate diff --git a/Source/LinkLabel.swift b/Source/LinkLabel.swift index 3e096bb..0b8b5d1 100644 --- a/Source/LinkLabel.swift +++ b/Source/LinkLabel.swift @@ -59,6 +59,10 @@ private class LinkAttribute { } } +extension NSAttributedStringKey { + // static NSAttributedStringKey +} + public protocol LinkLabelInteractionDelegate: class { func linkLabelDidSelectLink(linkLabel: LinkLabel, url: URL) } @@ -69,14 +73,14 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate { fileprivate var standardTextAttributes: Array = [] - open var linkTextAttributes: Dictionary { + open var linkTextAttributes: Dictionary { didSet { self.setupAttributes() } } //Text attributes displayed when a link has been highlighted - open var highlightedLinkTextAttributes: Dictionary { + open var highlightedLinkTextAttributes: Dictionary { didSet { self.setupAttributes() } @@ -109,13 +113,13 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate { self.attributedText!.enumerateAttributes(in: range, options: [], using: { (attributes, range, _) in for attribute in attributes { - if attribute.key == NSLinkAttributeName { + if attribute.key == .link { if attribute.value is URL { let linkAttribute = LinkAttribute(url: attribute.value as! URL, range: range) linkAttributes.append(linkAttribute) } } else { - let attribute = Attribute(attributeName: attribute.key, value: attribute.value as AnyObject, range: range) + let attribute = Attribute(attributeName: attribute.key.rawValue, value: attribute.value as AnyObject, range: range) standardAttributes.append(attribute) } } @@ -138,10 +142,12 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate { override public init(frame: CGRect) { linkTextAttributes = [ - NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int)] + .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int) + ] highlightedLinkTextAttributes = [ - NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int)] + .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int) + ] super.init(frame: frame) @@ -179,7 +185,7 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate { return nil } - func respondToLinkLabelTouched(_ gestureRecognizer: TouchGestureRecognizer) { + @objc func respondToLinkLabelTouched(_ gestureRecognizer: TouchGestureRecognizer) { if self.linkAttributes.count == 0 { return } @@ -205,7 +211,7 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate { } - func respondToLinkLabelTapped(_ gestureRecognizer: UITapGestureRecognizer) { + @objc func respondToLinkLabelTapped(_ gestureRecognizer: UITapGestureRecognizer) { if self.linkAttributes.count == 0 { return } @@ -234,17 +240,21 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate { mutableAttributedText.removeAttributes() for attribute in self.standardTextAttributes { - mutableAttributedText.addAttribute(attribute.attributeName, value: attribute.value, range: attribute.range) + mutableAttributedText.addAttribute( + NSAttributedStringKey(rawValue: attribute.attributeName), + value: attribute.value, + range: attribute.range + ) } for linkAttribute in self.linkAttributes { if linkAttribute === self.highlightedLinkAttribute { - for (attributeName, value): (String, AnyObject) in self.highlightedLinkTextAttributes { - mutableAttributedText.addAttribute(attributeName, value: value, range: linkAttribute.range) + for (attribute, value) in self.highlightedLinkTextAttributes { + mutableAttributedText.addAttribute(attribute, value: value, range: linkAttribute.range) } } else { - for (attributeName, value): (String, AnyObject) in self.linkTextAttributes { - mutableAttributedText.addAttribute(attributeName, value: value, range: linkAttribute.range) + for (attribute, value)in self.linkTextAttributes { + mutableAttributedText.addAttribute(attribute, value: value, range: linkAttribute.range) } } } diff --git a/Source/NSMutableAttributedString+RemoveAttributes.swift b/Source/NSMutableAttributedString+RemoveAttributes.swift index e269b64..5a07c4e 100644 --- a/Source/NSMutableAttributedString+RemoveAttributes.swift +++ b/Source/NSMutableAttributedString+RemoveAttributes.swift @@ -15,8 +15,7 @@ extension NSMutableAttributedString { } func removeAttributes(range: NSRange) { - self.enumerateAttributes(in: range, options: []) { - (attributes: [String: Any], range: NSRange, _) in + self.enumerateAttributes(in: range, options: []) { (attributes, range, _) in for attribute in attributes { self.removeAttribute(attribute.key, range: range) } From cd4acbddd3484f0eed490e2a315876843acf820a Mon Sep 17 00:00:00 2001 From: Felipe Espinoza Date: Thu, 9 Nov 2017 14:41:50 +0100 Subject: [PATCH 3/6] improve code style --- Source/UIGestureRecognizer+UILabel.swift | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Source/UIGestureRecognizer+UILabel.swift b/Source/UIGestureRecognizer+UILabel.swift index 1d4b7f8..b108882 100644 --- a/Source/UIGestureRecognizer+UILabel.swift +++ b/Source/UIGestureRecognizer+UILabel.swift @@ -20,13 +20,13 @@ extension UIGestureRecognizer { public extension UILabel { public func indexOfCharacter(atPoint point: CGPoint) -> Int? { - if self.attributedText == nil { + guard let attributedText = self.attributedText else { return nil } let layoutManager = NSLayoutManager() let textContainer = NSTextContainer() - let textStorage = NSTextStorage(attributedString: self.attributedText!) + let textStorage = NSTextStorage(attributedString: attributedText) layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) @@ -44,14 +44,17 @@ public extension UILabel { let textContainerOffset = CGPoint( x: (self.bounds.size.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, - y: (self.bounds.size.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y) + y: (self.bounds.size.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y + ) let locationOfTouchInTextContainer = CGPoint( x: point.x - textContainerOffset.x, - y: point.y - textContainerOffset.y) + y: point.y - textContainerOffset.y + ) let indexOfCharacter = layoutManager.characterIndex( for: locationOfTouchInTextContainer, in: textContainer, - fractionOfDistanceBetweenInsertionPoints: nil) + fractionOfDistanceBetweenInsertionPoints: nil + ) return indexOfCharacter } From c5f1c0b64c523fcf79ddb81eafd15e56bc7b3ec7 Mon Sep 17 00:00:00 2001 From: Felipe Espinoza Date: Thu, 9 Nov 2017 14:48:23 +0100 Subject: [PATCH 4/6] Remove check of the `point` in `textBoundingBox` The `textBoundingBox` is the rectangle that is needed to display the full attributed text in the cell and has the right dimensions, but the point is relative to the `frame` coordinates so the `point` is never contained in the in the `textBoundingBox` because *it represents the size of the text in the label, **but not the position of the text in the label**.* So this code is giving false negative checks --- Source/UIGestureRecognizer+UILabel.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Source/UIGestureRecognizer+UILabel.swift b/Source/UIGestureRecognizer+UILabel.swift index b108882..c6438de 100644 --- a/Source/UIGestureRecognizer+UILabel.swift +++ b/Source/UIGestureRecognizer+UILabel.swift @@ -37,11 +37,7 @@ public extension UILabel { textContainer.size = self.bounds.size let textBoundingBox = layoutManager.usedRect(for: textContainer) - - if !textBoundingBox.contains(point) { - return nil - } - + let textContainerOffset = CGPoint( x: (self.bounds.size.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, y: (self.bounds.size.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y From 32361b058f04dca7e241e312579fe74815312fcd Mon Sep 17 00:00:00 2001 From: Felipe Espinoza Date: Thu, 9 Nov 2017 14:52:49 +0100 Subject: [PATCH 5/6] Add readme to the Xcode project and update it to swift 4 --- LinkLabelDemo.xcodeproj/project.pbxproj | 4 ++++ LinkLabelDemo/ViewController.swift | 5 +++-- README.md | 18 +++++++++--------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/LinkLabelDemo.xcodeproj/project.pbxproj b/LinkLabelDemo.xcodeproj/project.pbxproj index 6de3373..1b84a72 100644 --- a/LinkLabelDemo.xcodeproj/project.pbxproj +++ b/LinkLabelDemo.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 4940B3211C04BF7F000524A0 /* TouchGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4940B31F1C04BF7F000524A0 /* TouchGestureRecognizer.swift */; }; 4940B3261C04C00F000524A0 /* UIGestureRecognizer+UILabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4940B3251C04C00F000524A0 /* UIGestureRecognizer+UILabel.swift */; }; 4940B3281C04C02B000524A0 /* NSMutableAttributedString+RemoveAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4940B3271C04C02B000524A0 /* NSMutableAttributedString+RemoveAttributes.swift */; }; + 9BBA120B1FB494820066F093 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 9BBA120A1FB494820066F093 /* README.md */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -30,6 +31,7 @@ 4940B31F1C04BF7F000524A0 /* TouchGestureRecognizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TouchGestureRecognizer.swift; path = Source/TouchGestureRecognizer.swift; sourceTree = SOURCE_ROOT; }; 4940B3251C04C00F000524A0 /* UIGestureRecognizer+UILabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIGestureRecognizer+UILabel.swift"; path = "Source/UIGestureRecognizer+UILabel.swift"; sourceTree = SOURCE_ROOT; }; 4940B3271C04C02B000524A0 /* NSMutableAttributedString+RemoveAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSMutableAttributedString+RemoveAttributes.swift"; path = "Source/NSMutableAttributedString+RemoveAttributes.swift"; sourceTree = SOURCE_ROOT; }; + 9BBA120A1FB494820066F093 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -46,6 +48,7 @@ 4940B2FF1C04BF3C000524A0 = { isa = PBXGroup; children = ( + 9BBA120A1FB494820066F093 /* README.md */, 4940B30A1C04BF3C000524A0 /* LinkLabelDemo */, 4940B3091C04BF3C000524A0 /* Products */, ); @@ -146,6 +149,7 @@ files = ( 4940B3161C04BF3C000524A0 /* LaunchScreen.storyboard in Resources */, 4940B3131C04BF3C000524A0 /* Assets.xcassets in Resources */, + 9BBA120B1FB494820066F093 /* README.md in Resources */, 4940B3111C04BF3C000524A0 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/LinkLabelDemo/ViewController.swift b/LinkLabelDemo/ViewController.swift index 32d8117..13a746c 100644 --- a/LinkLabelDemo/ViewController.swift +++ b/LinkLabelDemo/ViewController.swift @@ -17,14 +17,15 @@ class ViewController: UIViewController, LinkLabelInteractionDelegate { let text = "This is some text, which incidentally includes a link you may find interesting." let fullRange = NSMakeRange(0, (text as NSString).length) let linkRange = (text as NSString).range(of: "includes a link") + let attributedString = NSMutableAttributedString(string: text) attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 15), range: fullRange) attributedString.addAttribute(.foregroundColor, value: UIColor.black, range: fullRange) attributedString.addAttribute(.link, value: URL(string: "https://google.com")!, range: linkRange) let linkTextAttributes: [NSAttributedStringKey: AnyObject] = [ - NSAttributedStringKey.underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), - NSAttributedStringKey.foregroundColor: UIColor.green + .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), + .foregroundColor: UIColor.green ] let highlightedLinkTextAttributes: [NSAttributedStringKey: AnyObject] = [ diff --git a/README.md b/README.md index 9fdb799..3f5e258 100644 --- a/README.md +++ b/README.md @@ -36,22 +36,22 @@ let fullRange = NSMakeRange(0, (text as NSString).length) let linkRange = (text as NSString).rangeOfString("includes a link") let attributedString = NSMutableAttributedString(string: text) -attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: fullRange) -attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: fullRange) -attributedString.addAttribute(NSLinkAttributeName, value: NSURL(string: "https://google.com")!, range: linkRange) +attributedString.addAttribute(.font, value: UIFont.systemFont(ofSize: 15), range: fullRange) +attributedString.addAttribute(.foregroundColor, value: UIColor.black, range: fullRange) +attributedString.addAttribute(.link, value: URL(string: "https://google.com")!, range: linkRange) ``` If you wish to customise the link appearance: ```swift -let linkTextAttributes = [ - NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue), - NSForegroundColorAttributeName: UIColor.greenColor() +let linkTextAttributes: [NSAttributedStringKey: AnyObject] = [ + .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), + .foregroundColor: UIColor.green ] -let highlightedLinkTextAttributes = [ - NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue), - NSForegroundColorAttributeName: UIColor.redColor() +let highlightedLinkTextAttributes: [NSAttributedStringKey: AnyObject] = [ + .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int), + .foregroundColor: UIColor.red ] label.linkTextAttributes = linkTextAttributes From 7304278a9ee3b83caa957c3566fd36346d6bbeb7 Mon Sep 17 00:00:00 2001 From: Felipe Espinoza Date: Thu, 25 Jan 2018 11:20:24 +0100 Subject: [PATCH 6/6] Use type inferred context --- Source/TouchGestureRecognizer.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/TouchGestureRecognizer.swift b/Source/TouchGestureRecognizer.swift index 4782622..f11ecb4 100644 --- a/Source/TouchGestureRecognizer.swift +++ b/Source/TouchGestureRecognizer.swift @@ -10,20 +10,20 @@ import UIKit class TouchGestureRecognizer: UIGestureRecognizer { override func touchesBegan(_ touches: Set, with event: UIEvent) { - if self.state == UIGestureRecognizerState.possible { - self.state = UIGestureRecognizerState.began + if self.state == .possible { + self.state = .began } } override func touchesMoved(_ touches: Set, with event: UIEvent) { - self.state = UIGestureRecognizerState.changed + self.state = .changed } override func touchesEnded(_ touches: Set, with event: UIEvent) { - self.state = UIGestureRecognizerState.ended + self.state = .ended } override func touchesCancelled(_ touches: Set, with event: UIEvent) { - self.state = UIGestureRecognizerState.cancelled + self.state = .cancelled } }