Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 4 #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.0
31 changes: 27 additions & 4 deletions LinkLabelDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -46,6 +48,7 @@
4940B2FF1C04BF3C000524A0 = {
isa = PBXGroup;
children = (
9BBA120A1FB494820066F093 /* README.md */,
4940B30A1C04BF3C000524A0 /* LinkLabelDemo */,
4940B3091C04BF3C000524A0 /* Products */,
);
Expand Down Expand Up @@ -111,12 +114,13 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0910;
ORGANIZATIONNAME = "Project Dent";
TargetAttributes = {
4940B3071C04BF3C000524A0 = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0800;
DevelopmentTeam = 2Z7R9FQBNN;
LastSwiftMigration = 0910;
};
};
};
Expand Down Expand Up @@ -145,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;
Expand Down Expand Up @@ -195,14 +200,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 @@ -230,6 +241,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -242,14 +254,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 @@ -270,6 +288,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;
};
Expand All @@ -280,6 +299,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";
Expand All @@ -288,7 +308,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;
};
Expand All @@ -297,14 +318,16 @@
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";
PRODUCT_BUNDLE_IDENTIFIER = ProjectDent.LinkLabelDemo;
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;
};
Expand Down
28 changes: 12 additions & 16 deletions LinkLabelDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,37 @@ 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] = [
.underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int),
.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
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@ 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)
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:

```
let linkTextAttributes = [
NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue),
NSForegroundColorAttributeName: UIColor.greenColor()
```swift
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
Expand All @@ -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)")
}
Expand Down
36 changes: 23 additions & 13 deletions Source/LinkLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ private class LinkAttribute {
}
}

extension NSAttributedStringKey {
// static NSAttributedStringKey
}

public protocol LinkLabelInteractionDelegate: class {
func linkLabelDidSelectLink(linkLabel: LinkLabel, url: URL)
}
Expand All @@ -69,14 +73,14 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate {

fileprivate var standardTextAttributes: Array<Attribute> = []

open var linkTextAttributes: Dictionary<String, AnyObject> {
open var linkTextAttributes: Dictionary<NSAttributedStringKey, AnyObject> {
didSet {
self.setupAttributes()
}
}

//Text attributes displayed when a link has been highlighted
open var highlightedLinkTextAttributes: Dictionary<String, AnyObject> {
open var highlightedLinkTextAttributes: Dictionary<NSAttributedStringKey, AnyObject> {
didSet {
self.setupAttributes()
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -205,7 +211,7 @@ open class LinkLabel: UILabel, UIGestureRecognizerDelegate {

}

func respondToLinkLabelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
@objc func respondToLinkLabelTapped(_ gestureRecognizer: UITapGestureRecognizer) {
if self.linkAttributes.count == 0 {
return
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions Source/NSMutableAttributedString+RemoveAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading