-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from zacwest/long-press
Long-press recognition
- Loading branch information
Showing
12 changed files
with
430 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Example/ZSWTappableLabel/LongPressObjectiveCViewController.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// LongPressObjectiveCViewController.h | ||
// ZSWTappableLabel | ||
// | ||
// Created by Zachary West on 12/20/15. | ||
// Copyright © 2015 Zachary West. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface LongPressObjectiveCViewController : UIViewController | ||
|
||
@end |
95 changes: 95 additions & 0 deletions
95
Example/ZSWTappableLabel/LongPressObjectiveCViewController.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// LongPressObjectiveCViewController.m | ||
// ZSWTappableLabel | ||
// | ||
// Created by Zachary West on 12/20/15. | ||
// Copyright © 2015 Zachary West. All rights reserved. | ||
// | ||
|
||
#import "LongPressObjectiveCViewController.h" | ||
|
||
@import Masonry; | ||
@import ZSWTappableLabel; | ||
@import ZSWTaggedString; | ||
@import SafariServices; | ||
|
||
static NSString *const URLAttributeName = @"URL"; | ||
|
||
@interface LongPressObjectiveCViewController() <ZSWTappableLabelTapDelegate, ZSWTappableLabelLongPressDelegate> | ||
@property (nonatomic) ZSWTappableLabel *label; | ||
@end | ||
|
||
@implementation LongPressObjectiveCViewController | ||
|
||
- (void)viewDidLoad { | ||
[super viewDidLoad]; | ||
|
||
self.view.backgroundColor = [UIColor whiteColor]; | ||
|
||
self.label = ^{ | ||
ZSWTappableLabel *label = [[ZSWTappableLabel alloc] init]; | ||
label.textAlignment = NSTextAlignmentJustified; | ||
label.tapDelegate = self; | ||
label.longPressDelegate = self; | ||
label.longPressAccessibilityActionName = NSLocalizedString(@"Share", nil); | ||
return label; | ||
}(); | ||
|
||
ZSWTaggedStringOptions *options = [ZSWTaggedStringOptions options]; | ||
[options setDynamicAttributes:^NSDictionary *(NSString *tagName, | ||
NSDictionary *tagAttributes, | ||
NSDictionary *existingStringAttributes) { | ||
NSURL *URL; | ||
if ([tagAttributes[@"type"] isEqualToString:@"privacy"]) { | ||
URL = [NSURL URLWithString:@"http://google.com/search?q=privacy"]; | ||
} else if ([tagAttributes[@"type"] isEqualToString:@"tos"]) { | ||
URL = [NSURL URLWithString:@"http://google.com/search?q=tos"]; | ||
} | ||
|
||
if (!URL) { | ||
return nil; | ||
} | ||
|
||
return @{ | ||
ZSWTappableLabelTappableRegionAttributeName: @YES, | ||
ZSWTappableLabelHighlightedBackgroundAttributeName: [UIColor lightGrayColor], | ||
ZSWTappableLabelHighlightedForegroundAttributeName: [UIColor whiteColor], | ||
NSForegroundColorAttributeName: [UIColor blueColor], | ||
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), | ||
@"URL": URL | ||
}; | ||
} forTagName:@"link"]; | ||
|
||
NSString *string = NSLocalizedString(@"Please, feel free to peruse and enjoy our wonderful and alluring <link type='privacy'>Privacy Policy</link> or if you'd really like to understand what you're allowed or not allowed to do, reading our <link type='tos'>Terms of Service</link> is sure to be enlightening", nil); | ||
self.label.attributedText = [[ZSWTaggedString stringWithString:string] attributedStringWithOptions:options]; | ||
|
||
[self.view addSubview:self.label]; | ||
[self.label mas_makeConstraints:^(MASConstraintMaker *make) { | ||
make.edges.equalTo(self.view); | ||
}]; | ||
} | ||
|
||
#pragma mark - ZSWTappableLabelTapDelegate | ||
|
||
- (void)tappableLabel:(ZSWTappableLabel *)tappableLabel tappedAtIndex:(NSInteger)idx withAttributes:(NSDictionary<NSString *,id> *)attributes { | ||
NSURL *URL = attributes[URLAttributeName]; | ||
if ([URL isKindOfClass:[NSURL class]]) { | ||
if ([SFSafariViewController class] != nil) { | ||
[self showViewController:[[SFSafariViewController alloc] initWithURL:URL] sender:self]; | ||
} else { | ||
[[UIApplication sharedApplication] openURL:URL]; | ||
} | ||
} | ||
} | ||
|
||
#pragma mark - ZSWTappableLabelLongPressDelegate | ||
|
||
- (void)tappableLabel:(ZSWTappableLabel *)tappableLabel longPressedAtIndex:(NSInteger)idx withAttributes:(NSDictionary<NSString *,id> *)attributes { | ||
NSURL *URL = attributes[URLAttributeName]; | ||
if ([URL isKindOfClass:[NSURL class]]) { | ||
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[ URL ] applicationActivities:nil]; | ||
[self presentViewController:activityController animated:YES completion:nil]; | ||
} | ||
} | ||
|
||
@end |
95 changes: 95 additions & 0 deletions
95
Example/ZSWTappableLabel/LongPressSwiftViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// LongPressSwiftViewController.swift | ||
// ZSWTappableLabel | ||
// | ||
// Created by Zachary West on 12/20/15. | ||
// Copyright © 2015 Zachary West. All rights reserved. | ||
// | ||
|
||
import ZSWTappableLabel | ||
import ZSWTaggedString | ||
import SafariServices | ||
|
||
class LongPressSwiftViewController: UIViewController, ZSWTappableLabelTapDelegate, ZSWTappableLabelLongPressDelegate { | ||
let label: ZSWTappableLabel = { | ||
let label = ZSWTappableLabel() | ||
label.textAlignment = .Justified | ||
return label | ||
}() | ||
|
||
static let URLAttributeName = "URL" | ||
|
||
enum LinkType: String { | ||
case Privacy = "privacy" | ||
case TermsOfService = "tos" | ||
|
||
var URL: NSURL { | ||
switch self { | ||
case .Privacy: | ||
return NSURL(string: "http://google.com/search?q=privacy")! | ||
case .TermsOfService: | ||
return NSURL(string: "http://google.com/search?q=tos")! | ||
} | ||
} | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
view.backgroundColor = UIColor.whiteColor() | ||
|
||
label.tapDelegate = self | ||
label.longPressDelegate = self | ||
label.longPressAccessibilityActionName = NSLocalizedString("Share", comment: "") | ||
|
||
let options = ZSWTaggedStringOptions() | ||
options["link"] = .Dynamic({ tagName, tagAttributes, stringAttributes in | ||
guard let typeString = tagAttributes["type"] as? String, | ||
let type = LinkType(rawValue: typeString) else { | ||
return [String: AnyObject]() | ||
} | ||
|
||
return [ | ||
ZSWTappableLabelTappableRegionAttributeName: true, | ||
ZSWTappableLabelHighlightedBackgroundAttributeName: UIColor.lightGrayColor(), | ||
ZSWTappableLabelHighlightedForegroundAttributeName: UIColor.whiteColor(), | ||
NSForegroundColorAttributeName: UIColor.blueColor(), | ||
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue, | ||
MultipleSwiftViewController.URLAttributeName: type.URL | ||
] | ||
}) | ||
|
||
let string = NSLocalizedString("Please, feel free to peruse and enjoy our wonderful and alluring <link type='privacy'>Privacy Policy</link> or if you'd really like to understand what you're allowed or not allowed to do, reading our <link type='tos'>Terms of Service</link> is sure to be enlightening", comment: "") | ||
label.attributedText = try? ZSWTaggedString(string: string).attributedStringWithOptions(options) | ||
|
||
view.addSubview(label) | ||
label.snp_makeConstraints { make in | ||
make.edges.equalTo(view) | ||
} | ||
} | ||
|
||
// MARK: - ZSWTappableLabelTapDelegate | ||
|
||
func tappableLabel(tappableLabel: ZSWTappableLabel, tappedAtIndex idx: Int, withAttributes attributes: [String : AnyObject]) { | ||
guard let URL = attributes[SimpleSwiftViewController.URLAttributeName] as? NSURL else { | ||
return | ||
} | ||
|
||
if #available(iOS 9, *) { | ||
showViewController(SFSafariViewController(URL: URL), sender: self) | ||
} else { | ||
UIApplication.sharedApplication().openURL(URL) | ||
} | ||
} | ||
|
||
// MARK: - ZSWTappableLabelLongPressDelegate | ||
|
||
func tappableLabel(tappableLabel: ZSWTappableLabel, longPressedAtIndex idx: Int, withAttributes attributes: [String : AnyObject]) { | ||
guard let URL = attributes[SimpleSwiftViewController.URLAttributeName] as? NSURL else { | ||
return | ||
} | ||
|
||
let activityController = UIActivityViewController(activityItems: [URL], applicationActivities: nil) | ||
presentViewController(activityController, animated: true, completion: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.