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

Bug fixes, improved link handling and Swift 5 support #8

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update project to swift 4
compiled with xcode 9.1
  • Loading branch information
fespinoza committed Nov 9, 2017
commit 156027913e4505ca48e0e74a5bfd6484d0d8a426
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.0
27 changes: 23 additions & 4 deletions LinkLabelDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
};
Expand All @@ -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";
Expand All @@ -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;
};
Expand All @@ -297,14 +314,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
27 changes: 11 additions & 16 deletions LinkLabelDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
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