diff --git a/CCHLinkTextView Example/CCHLinkTextView Example.xcodeproj/project.pbxproj b/CCHLinkTextView Example/CCHLinkTextView Example.xcodeproj/project.pbxproj index c6089a1..5badf69 100644 --- a/CCHLinkTextView Example/CCHLinkTextView Example.xcodeproj/project.pbxproj +++ b/CCHLinkTextView Example/CCHLinkTextView Example.xcodeproj/project.pbxproj @@ -9,8 +9,8 @@ /* Begin PBXBuildFile section */ 623AC49518C11BAD000962A0 /* CCHLinkTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 623AC49418C11BAD000962A0 /* CCHLinkTextView.m */; }; 626F837918C75FBC004FEABB /* CCHLinkTextViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 626F837818C75FBC004FEABB /* CCHLinkTextViewTests.m */; }; - 627D34AD18D0C18B00A1CB9E /* TextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 627D34AC18D0C18B00A1CB9E /* TextView.m */; }; 629754F318CB69F90050DCFE /* CCHLinkGestureRecognizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 629754F218CB69F90050DCFE /* CCHLinkGestureRecognizer.m */; }; + 62A3423618FC555B00003EC3 /* TableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A3423518FC555B00003EC3 /* TableViewCell.m */; }; 62F55CC118C1180200A7E1CC /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC018C1180200A7E1CC /* Foundation.framework */; }; 62F55CC318C1180200A7E1CC /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC218C1180200A7E1CC /* CoreGraphics.framework */; }; 62F55CC518C1180200A7E1CC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 62F55CC418C1180200A7E1CC /* UIKit.framework */; }; @@ -41,10 +41,10 @@ 623AC49418C11BAD000962A0 /* CCHLinkTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCHLinkTextView.m; sourceTree = ""; }; 623AC49618C11EAA000962A0 /* CCHLinkTextViewDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CCHLinkTextViewDelegate.h; sourceTree = ""; }; 626F837818C75FBC004FEABB /* CCHLinkTextViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCHLinkTextViewTests.m; sourceTree = ""; }; - 627D34AB18D0C18B00A1CB9E /* TextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextView.h; sourceTree = ""; }; - 627D34AC18D0C18B00A1CB9E /* TextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TextView.m; sourceTree = ""; }; 629754F118CB69F90050DCFE /* CCHLinkGestureRecognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCHLinkGestureRecognizer.h; sourceTree = ""; }; 629754F218CB69F90050DCFE /* CCHLinkGestureRecognizer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCHLinkGestureRecognizer.m; sourceTree = ""; }; + 62A3423418FC555B00003EC3 /* TableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TableViewCell.h; sourceTree = ""; }; + 62A3423518FC555B00003EC3 /* TableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TableViewCell.m; sourceTree = ""; }; 62F55CBD18C1180200A7E1CC /* CCHLinkTextView Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "CCHLinkTextView Example.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 62F55CC018C1180200A7E1CC /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 62F55CC218C1180200A7E1CC /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; @@ -141,8 +141,8 @@ 62F55CD218C1180200A7E1CC /* Main.storyboard */, 62F55CD518C1180200A7E1CC /* ViewController.h */, 62F55CD618C1180200A7E1CC /* ViewController.m */, - 627D34AB18D0C18B00A1CB9E /* TextView.h */, - 627D34AC18D0C18B00A1CB9E /* TextView.m */, + 62A3423418FC555B00003EC3 /* TableViewCell.h */, + 62A3423518FC555B00003EC3 /* TableViewCell.m */, 62F55CD818C1180200A7E1CC /* Images.xcassets */, 62F55CC718C1180200A7E1CC /* Supporting Files */, ); @@ -276,9 +276,9 @@ buildActionMask = 2147483647; files = ( 623AC49518C11BAD000962A0 /* CCHLinkTextView.m in Sources */, + 62A3423618FC555B00003EC3 /* TableViewCell.m in Sources */, 62F55CD718C1180200A7E1CC /* ViewController.m in Sources */, 629754F318CB69F90050DCFE /* CCHLinkGestureRecognizer.m in Sources */, - 627D34AD18D0C18B00A1CB9E /* TextView.m in Sources */, 62F55CD118C1180200A7E1CC /* AppDelegate.m in Sources */, 62F55CCD18C1180200A7E1CC /* main.m in Sources */, ); diff --git a/CCHLinkTextView Example/CCHLinkTextView Example/Base.lproj/Main.storyboard b/CCHLinkTextView Example/CCHLinkTextView Example/Base.lproj/Main.storyboard index 9ae570f..44082d7 100644 --- a/CCHLinkTextView Example/CCHLinkTextView Example/Base.lproj/Main.storyboard +++ b/CCHLinkTextView Example/CCHLinkTextView Example/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -8,74 +8,76 @@ - + - - - - - - - - - - - - - - - Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. - - - - - - - - - - - - - - - - - - - - - - - - - - - Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. - - - - - - - - - - - - + + + + + + + + + + + + + + This is a CCHLinkTextView using CCHLinkAttributeName. You can tap or long press links and a message will appear in the nav bar. Long pressing the entire cell is handled by a UILongPressGesture-Recognizer added to the table view. + + + + + + + + + + + + + + + + + + + + + + + + + + + + This is a UITextView with NSLinkAttributeName. It can handle taps, but has an annoying delay when recognizing this gesture. Try tapping quickly on a link in this text view. Also, it can't handle long presses on links. + + + + + + + + + + + + + + + + + + - - - - @@ -125,6 +127,6 @@ - + diff --git a/CCHLinkTextView Example/CCHLinkTextView Example/TableViewCell.h b/CCHLinkTextView Example/CCHLinkTextView Example/TableViewCell.h new file mode 100644 index 0000000..4dfa65e --- /dev/null +++ b/CCHLinkTextView Example/CCHLinkTextView Example/TableViewCell.h @@ -0,0 +1,15 @@ +// +// TableViewCell.h +// CCHLinkTextView Example +// +// Created by Hoefele, Claus on 14.04.14. +// Copyright (c) 2014 Claus Höfele. All rights reserved. +// + +#import + +@interface TableViewCell : UITableViewCell + +@property (weak, nonatomic) IBOutlet UITextView *textView; + +@end diff --git a/CCHLinkTextView Example/CCHLinkTextView Example/TableViewCell.m b/CCHLinkTextView Example/CCHLinkTextView Example/TableViewCell.m new file mode 100644 index 0000000..d578782 --- /dev/null +++ b/CCHLinkTextView Example/CCHLinkTextView Example/TableViewCell.m @@ -0,0 +1,13 @@ +// +// TableViewCell.m +// CCHLinkTextView Example +// +// Created by Hoefele, Claus on 14.04.14. +// Copyright (c) 2014 Claus Höfele. All rights reserved. +// + +#import "TableViewCell.h" + +@implementation TableViewCell + +@end diff --git a/CCHLinkTextView Example/CCHLinkTextView Example/TextView.h b/CCHLinkTextView Example/CCHLinkTextView Example/TextView.h deleted file mode 100644 index d76db9a..0000000 --- a/CCHLinkTextView Example/CCHLinkTextView Example/TextView.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// TextView.h -// Stolpersteine -// -// Created by Claus Höfele on 03.03.14. -// Copyright (c) 2014 Option-U Software. All rights reserved. -// - -#import - -@interface TextView : UITextView - -@property (nonatomic, weak) UIViewController *viewController; - -@end diff --git a/CCHLinkTextView Example/CCHLinkTextView Example/TextView.m b/CCHLinkTextView Example/CCHLinkTextView Example/TextView.m deleted file mode 100644 index b083e8c..0000000 --- a/CCHLinkTextView Example/CCHLinkTextView Example/TextView.m +++ /dev/null @@ -1,20 +0,0 @@ -// -// TextView.m -// Stolpersteine -// -// Created by Claus Höfele on 03.03.14. -// Copyright (c) 2014 Option-U Software. All rights reserved. -// - -#import "TextView.h" - -@implementation TextView - -- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -{ - [super touchesBegan:touches withEvent:event]; - - [self.viewController performSegueWithIdentifier:@"tableViewToDetail" sender:self]; -} - -@end diff --git a/CCHLinkTextView Example/CCHLinkTextView Example/ViewController.m b/CCHLinkTextView Example/CCHLinkTextView Example/ViewController.m index 43a850b..3bdfa74 100644 --- a/CCHLinkTextView Example/CCHLinkTextView Example/ViewController.m +++ b/CCHLinkTextView Example/CCHLinkTextView Example/ViewController.m @@ -9,12 +9,11 @@ #import "CCHLinkTextViewDelegate.h" #import "CCHLinkGestureRecognizer.h" -#import "TextView.h" +#import "TableViewCell.h" @interface ViewController () -@property (weak, nonatomic) IBOutlet CCHLinkTextView *linkTextView; -@property (weak, nonatomic) IBOutlet TextView *standardTextView; +@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGestureRecognizer; @end @@ -24,23 +23,7 @@ { [super viewDidLoad]; - [self setUpLinkTextView:self.linkTextView]; - [self setUpStandardTextView:self.standardTextView]; - - UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; - [longPressGestureRecognizer requireGestureRecognizerToFail:self.linkTextView.linkGestureRecognizer]; - [self.tableView addGestureRecognizer:longPressGestureRecognizer]; -} - -- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer -{ - CGPoint point = [gestureRecognizer locationInView:self.tableView]; - - NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; - if (indexPath) { - NSString *message = [NSString stringWithFormat:@"UITableView long pressed %zd", indexPath.row]; - [self showMessage:message]; - } + [self setUpLongPressGesture]; } - (void)showMessage:(NSString *)message @@ -48,6 +31,47 @@ self.navigationItem.title = message; } +#pragma mark - Table view handling + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ + return 10; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + TableViewCell *tableViewCell; + + NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0]; + if (indexPath.row < (numberOfRows - 1)) { + tableViewCell = [self.tableView dequeueReusableCellWithIdentifier:@"linkTextViewCell"]; + [self setUpLinkTextView:(CCHLinkTextView *)tableViewCell.textView]; + } else { + tableViewCell = [self.tableView dequeueReusableCellWithIdentifier:@"standardTextViewCell"]; + [self setUpStandardTextView:tableViewCell.textView]; + } + + return tableViewCell; +} + +#pragma mark - Long presses on table view cells + +- (void)setUpLongPressGesture +{ + self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; + [self.tableView addGestureRecognizer:self.longPressGestureRecognizer]; +} + +- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer +{ + CGPoint point = [gestureRecognizer locationInView:self.tableView]; + NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; + if (indexPath) { + NSString *message = [NSString stringWithFormat:@"Cell %zd long pressed", indexPath.row]; + [self showMessage:message]; + } +} + #pragma mark - CCHLinkAttributeName solution - (void)setUpLinkTextView:(CCHLinkTextView *)linkTextView @@ -57,37 +81,41 @@ NSMutableAttributedString *attributedText = [linkTextView.attributedText mutableCopy]; if (attributedText) { - [attributedText addAttribute:CCHLinkAttributeName value:@"1" range:NSMakeRange(0, 20)]; - [attributedText addAttribute:CCHLinkAttributeName value:@"2" range:NSMakeRange(5, 20)]; - [attributedText addAttribute:CCHLinkAttributeName value:@"3" range:NSMakeRange(120, 100)]; + [attributedText addAttribute:CCHLinkAttributeName value:@"0" range:NSMakeRange(0, 20)]; + [attributedText addAttribute:CCHLinkAttributeName value:@"1" range:NSMakeRange(5, 20)]; + [attributedText addAttribute:CCHLinkAttributeName value:@"2" range:NSMakeRange(150, 40)]; linkTextView.attributedText = attributedText; } + [self.longPressGestureRecognizer requireGestureRecognizerToFail:linkTextView.linkGestureRecognizer]; linkTextView.linkDelegate = self; } - (void)linkTextView:(CCHLinkTextView *)linkTextView didTapLinkWithValue:(id)value { - [self showMessage:[NSString stringWithFormat:@"CCHLinkAttributeName tapped %@", value]]; + CGPoint point = [self.tableView convertPoint:linkTextView.frame.origin fromView:linkTextView]; + NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; + [self showMessage:[NSString stringWithFormat:@"Cell %zd link %@ tapped", indexPath.row, value]]; } - (void)linkTextView:(CCHLinkTextView *)linkTextView didLongPressLinkWithValue:(id)value { - [self showMessage:[NSString stringWithFormat:@"CCHLinkAttributeName long pressed %@", value]]; + CGPoint point = [self.tableView convertPoint:linkTextView.frame.origin fromView:linkTextView]; + NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; + [self showMessage:[NSString stringWithFormat:@"Cell %zd link %@ long pressed", indexPath.row, value]]; } #pragma mark - NSLinkAttributeName solution -- (void)setUpStandardTextView:(TextView *)standardTextView +- (void)setUpStandardTextView:(UITextView *)standardTextView { standardTextView.editable = NO; - standardTextView.viewController = self; NSMutableAttributedString *attributedText = [standardTextView.attributedText mutableCopy]; if (attributedText) { - [attributedText addAttribute:NSLinkAttributeName value:@"1" range:NSMakeRange(0, 20)]; - [attributedText addAttribute:NSLinkAttributeName value:@"2" range:NSMakeRange(5, 20)]; - [attributedText addAttribute:NSLinkAttributeName value:@"3" range:NSMakeRange(120, 100)]; + [attributedText addAttribute:NSLinkAttributeName value:@"0" range:NSMakeRange(0, 20)]; + [attributedText addAttribute:NSLinkAttributeName value:@"1" range:NSMakeRange(5, 16)]; + [attributedText addAttribute:NSLinkAttributeName value:@"2" range:NSMakeRange(150, 40)]; standardTextView.attributedText = attributedText; } @@ -96,7 +124,10 @@ - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { - [self showMessage:[NSString stringWithFormat:@"NSLinkAttributeName tapped %@", URL]]; + CGPoint point = [self.tableView convertPoint:textView.frame.origin fromView:textView]; + NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; + [self showMessage:[NSString stringWithFormat:@"Cell %zd link %@ tapped", indexPath.row, URL]]; + return NO; }