From e8e0b6c772d1b9757be29042d8e1f19bf7edb3e8 Mon Sep 17 00:00:00 2001 From: Aleksey Tyurnin Date: Tue, 21 Jun 2016 18:14:57 +0500 Subject: [PATCH 1/2] UIAlertController added --- .../BasicInstantiationViewController.m | 19 +- src/NIAttributedLabel.h | 23 ++ src/NIAttributedLabel.m | 234 +++++++++++++----- 3 files changed, 208 insertions(+), 68 deletions(-) diff --git a/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m b/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m index f3bb262..a13dbf3 100644 --- a/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m +++ b/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m @@ -85,10 +85,19 @@ - (void)viewDidLoad { #pragma mark - NIAttributedLabelDelegate -- (void)attributedLabel:(NIAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point { - if (result.resultType == NSTextCheckingTypeLink) { - [[UIApplication sharedApplication] openURL:result.URL]; - } +//- (void)attributedLabel:(NIAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point { +// if (result.resultType == NSTextCheckingTypeLink) { +// [[UIApplication sharedApplication] openURL:result.URL]; +// } +//} + +#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 +#else +- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentAlertController:(UIAlertController *)alertController withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point{ + return true; } - +-(UIViewController*)attributedLabel:(NIAttributedLabel *)attributedLabel controllerToPresentAlertController:(UIAlertController *)alertController{ + return self; +} +#endif @end diff --git a/src/NIAttributedLabel.h b/src/NIAttributedLabel.h index 962d5cc..45c7392 100644 --- a/src/NIAttributedLabel.h +++ b/src/NIAttributedLabel.h @@ -129,6 +129,8 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText */ - (void)attributedLabel:(NIAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; + +#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 /** * Asks the receiver whether an action sheet should be displayed at the given point. * @@ -147,6 +149,27 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText * displayed. */ - (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentActionSheet:(UIActionSheet *)actionSheet withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; +#else +/** + * Asks the receiver whether an alert controller should be displayed at the given point. + * + * If this method is not implemented by the receiver then @c alertController will always be displayed if it can. + * + * @c alertController will be populated with actions that match the data type that was selected. For + * example, a link will have the actions "Open in Safari" and "Copy URL". A phone number will have + * @"Call" and "Copy Phone Number". + * + * @param attributedLabel An attributed label asking the delegate whether to display the action + * sheet. + * @param alertController The alert controller that will be displayed if YES is returned. + * @param result The data detector result that was selected. + * @param point The point within @c attributedLabel where the result was tapped. + * @returns YES if @c alertController should be displayed. NO if @c alertController should not be + * displayed. + */ +- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentAlertController:(UIAlertController *)alertController withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; +-(UIViewController*)attributedLabel:(NIAttributedLabel *)attributedLabel controllerToPresentAlertController:(UIAlertController *)alertController; +#endif @end diff --git a/src/NIAttributedLabel.m b/src/NIAttributedLabel.m index cc915a5..cb1769e 100644 --- a/src/NIAttributedLabel.m +++ b/src/NIAttributedLabel.m @@ -111,7 +111,11 @@ - (CGSize)boxSize { @end +#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 @interface NIAttributedLabel() +#else +@interface NIAttributedLabel() +#endif @property (nonatomic, strong) NSMutableAttributedString* mutableAttributedString; @@ -923,76 +927,178 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self setNeedsDisplay]; } +#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 - (UIActionSheet *)actionSheetForResult:(NSTextCheckingResult *)result { - UIActionSheet* actionSheet = - [[UIActionSheet alloc] initWithTitle:nil - delegate:self - cancelButtonTitle:nil - destructiveButtonTitle:nil - otherButtonTitles:nil]; - - NSString* title = nil; - if (NSTextCheckingTypeLink == result.resultType) { - if ([result.URL.scheme isEqualToString:@"mailto"]) { - title = result.URL.resourceSpecifier; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Mail", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Email Address", @"")]; - + UIActionSheet* actionSheet = + [[UIActionSheet alloc] initWithTitle:nil + delegate:self + cancelButtonTitle:nil + destructiveButtonTitle:nil + otherButtonTitles:nil]; + + NSString* title = nil; + if (NSTextCheckingTypeLink == result.resultType) { + if ([result.URL.scheme isEqualToString:@"mailto"]) { + title = result.URL.resourceSpecifier; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Mail", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Email Address", @"")]; + + } else { + title = result.URL.absoluteString; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Safari", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy URL", @"")]; + } + + } else if (NSTextCheckingTypePhoneNumber == result.resultType) { + title = result.phoneNumber; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Call", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Phone Number", @"")]; + + } else if (NSTextCheckingTypeAddress == result.resultType) { + title = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Maps", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Address", @"")]; + } else { - title = result.URL.absoluteString; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Safari", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy URL", @"")]; + // This type has not been implemented yet. + NI_DASSERT(NO); + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy", @"")]; } - - } else if (NSTextCheckingTypePhoneNumber == result.resultType) { - title = result.phoneNumber; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Call", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Phone Number", @"")]; - - } else if (NSTextCheckingTypeAddress == result.resultType) { - title = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Maps", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Address", @"")]; - - } else { - // This type has not been implemented yet. - NI_DASSERT(NO); - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy", @"")]; - } - actionSheet.title = title; - - if (!NIIsPad()) { - [actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")]]; - } - - return actionSheet; + actionSheet.title = title; + + if (!NIIsPad()) { + [actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")]]; + } + + return actionSheet; +} +#else +-(UIAlertController *)alertControllerForResult:(NSTextCheckingResult *)result { + UIAlertController *alertController = [[UIAlertController alloc] init]; + + NSString* title = nil; + if (NSTextCheckingTypeLink == result.resultType) { + if ([result.URL.scheme isEqualToString:@"mailto"]) { + title = result.URL.resourceSpecifier; + + UIAlertAction *mail = [UIAlertAction actionWithTitle:NSLocalizedString(@"Open in Mail", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIApplication sharedApplication] openURL:self.actionSheetLink.URL]; + }]; + UIAlertAction *copy = [UIAlertAction actionWithTitle:NSLocalizedString(@"Copy Email Address", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIPasteboard generalPasteboard] setString:self.actionSheetLink.URL.resourceSpecifier]; + }]; + + [alertController addAction:mail]; + [alertController addAction:copy]; + } else { + title = result.URL.absoluteString; + + UIAlertAction *link = [UIAlertAction actionWithTitle:NSLocalizedString(@"Open in Safari", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIApplication sharedApplication] openURL:self.actionSheetLink.URL]; + }]; + UIAlertAction *copy = [UIAlertAction actionWithTitle:NSLocalizedString(@"Copy URL", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIPasteboard generalPasteboard] setURL:self.actionSheetLink.URL]; + }]; + + [alertController addAction:link]; + [alertController addAction:copy]; + } + + } else if (NSTextCheckingTypePhoneNumber == result.resultType) { + title = result.phoneNumber; + + UIAlertAction *phone = [UIAlertAction actionWithTitle:NSLocalizedString(@"Call", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[@"tel:" stringByAppendingString:self.actionSheetLink.phoneNumber]]]; + }]; + UIAlertAction *copy = [UIAlertAction actionWithTitle:NSLocalizedString(@"Copy Phone Number", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIPasteboard generalPasteboard] setString:self.actionSheetLink.phoneNumber]; + }]; + + [alertController addAction:phone]; + [alertController addAction:copy]; + + } else if (NSTextCheckingTypeAddress == result.resultType) { + title = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; + + NSString* address = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; + UIAlertAction *phone = [UIAlertAction actionWithTitle:NSLocalizedString(@"Open in Maps", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[@"http://maps.google.com/maps?q=" stringByAppendingString:address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; + }]; + UIAlertAction *copy = [UIAlertAction actionWithTitle:NSLocalizedString(@"Copy Address", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIPasteboard generalPasteboard] setString:address]; + }]; + + [alertController addAction:phone]; + [alertController addAction:copy]; + + } else { + // This type has not been implemented yet. + NI_DASSERT(NO); + NSString* text = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; + UIAlertAction *copy = [UIAlertAction actionWithTitle:NSLocalizedString(@"Copy", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [[UIPasteboard generalPasteboard] setString:text]; + }]; + [alertController addAction:copy]; + } + + alertController.title = title; + + if (!NIIsPad()) { + + UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { + }]; + [alertController addAction:cancel]; + // [actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")]]; + } + + return alertController; } +#endif - (void)_longPressTimerDidFire:(NSTimer *)timer { - self.longPressTimer = nil; - - if (nil != self.touchedLink) { - self.actionSheetLink = self.touchedLink; - - UIActionSheet* actionSheet = [self actionSheetForResult:self.actionSheetLink]; - - BOOL shouldPresent = YES; - if ([self.delegate respondsToSelector:@selector(attributedLabel:shouldPresentActionSheet:withTextCheckingResult:atPoint:)]) { - // Give the delegate the opportunity to not show the action sheet or to present its own. - shouldPresent = [self.delegate attributedLabel:self shouldPresentActionSheet:actionSheet withTextCheckingResult:self.touchedLink atPoint:self.touchPoint]; - } - - if (shouldPresent) { - if (NIIsPad()) { - [actionSheet showFromRect:CGRectMake(self.touchPoint.x - 22, self.touchPoint.y - 22, 44, 44) inView:self animated:YES]; - } else { - [actionSheet showInView:self]; - } - - } else { - self.actionSheetLink = nil; + self.longPressTimer = nil; + + if (nil != self.touchedLink) { + self.actionSheetLink = self.touchedLink; + +#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 + UIActionSheet* actionSheet = [self actionSheetForResult:self.actionSheetLink]; + + BOOL shouldPresent = YES; + if ([self.delegate respondsToSelector:@selector(attributedLabel:shouldPresentActionSheet:withTextCheckingResult:atPoint:)]) { + // Give the delegate the opportunity to not show the action sheet or to present its own. + shouldPresent = [self.delegate attributedLabel:self shouldPresentActionSheet:actionSheet withTextCheckingResult:self.touchedLink atPoint:self.touchPoint]; + } + + if (shouldPresent) { + if (NIIsPad()) { + [actionSheet showFromRect:CGRectMake(self.touchPoint.x - 22, self.touchPoint.y - 22, 44, 44) inView:self animated:YES]; + } else { + [actionSheet showInView:self]; + } + + } else { + self.actionSheetLink = nil; + } +#else + UIAlertController *alertController = [self alertControllerForResult:self.actionSheetLink]; + + BOOL shouldPresent = YES; + if ([self.delegate respondsToSelector:@selector(attributedLabel:shouldPresentAlertController:withTextCheckingResult:atPoint:)]) { + shouldPresent = [self.delegate attributedLabel:self shouldPresentAlertController:alertController withTextCheckingResult:self.touchedLink atPoint:self.touchPoint]; + } + + if (shouldPresent) { + UIViewController *vc = nil; + if ([self.delegate respondsToSelector:@selector(attributedLabel:controllerToPresentAlertController:)]){ + vc = [self.delegate attributedLabel:self controllerToPresentAlertController:alertController]; + } + [vc presentViewController:alertController animated:true completion:nil]; + }else{ + self.actionSheetLink = nil; + } +#endif } - } } - (void)_applyLinkStyleWithResults:(NSArray *)results toAttributedString:(NSMutableAttributedString *)attributedString { @@ -1409,6 +1515,7 @@ - (NSInteger)indexOfAccessibilityElement:(id)element { #pragma mark - UIActionSheetDelegate +#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 - (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (NSTextCheckingTypeLink == self.actionSheetLink.resultType) { if (buttonIndex == 0) { @@ -1456,6 +1563,7 @@ - (void)actionSheetCancel:(UIActionSheet *)actionSheet { self.actionSheetLink = nil; [self setNeedsDisplay]; } +#endif #pragma mark - Inline Image Support From a183d0d87809da6a16197e10c6bcf52efd15bf16 Mon Sep 17 00:00:00 2001 From: Aleksey Tyurnin Date: Tue, 21 Jun 2016 18:25:21 +0500 Subject: [PATCH 2/2] fixed example fixed versions --- .../BasicInstantiationViewController.m | 13 +- src/NIAttributedLabel.h | 28 ++-- src/NIAttributedLabel.m | 139 +++++++++--------- 3 files changed, 89 insertions(+), 91 deletions(-) diff --git a/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m b/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m index a13dbf3..7cddb74 100644 --- a/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m +++ b/catalog/BasicInstantiation/BasicInstantiation/controllers/BasicInstantiationViewController.m @@ -85,19 +85,16 @@ - (void)viewDidLoad { #pragma mark - NIAttributedLabelDelegate -//- (void)attributedLabel:(NIAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point { -// if (result.resultType == NSTextCheckingTypeLink) { -// [[UIApplication sharedApplication] openURL:result.URL]; -// } -//} - -#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 -#else +#ifdef IF_IOS8_OR_GREATER - (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentAlertController:(UIAlertController *)alertController withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point{ return true; } -(UIViewController*)attributedLabel:(NIAttributedLabel *)attributedLabel controllerToPresentAlertController:(UIAlertController *)alertController{ return self; } +#else +- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentActionSheet:(UIActionSheet *)actionSheet withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point{ + return true; +} #endif @end diff --git a/src/NIAttributedLabel.h b/src/NIAttributedLabel.h index 45c7392..b2fefd8 100644 --- a/src/NIAttributedLabel.h +++ b/src/NIAttributedLabel.h @@ -130,45 +130,45 @@ extern NSString* const NIAttributedLabelLinkAttributeName; // Value is an NSText - (void)attributedLabel:(NIAttributedLabel *)attributedLabel didSelectTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; -#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 +#ifdef IF_IOS8_OR_GREATER /** - * Asks the receiver whether an action sheet should be displayed at the given point. + * Asks the receiver whether an alert controller should be displayed at the given point. * - * If this method is not implemented by the receiver then @c actionSheet will always be displayed. + * If this method is not implemented by the receiver then @c alertController will always be displayed if it can. * - * @c actionSheet will be populated with actions that match the data type that was selected. For + * @c alertController will be populated with actions that match the data type that was selected. For * example, a link will have the actions "Open in Safari" and "Copy URL". A phone number will have * @"Call" and "Copy Phone Number". * * @param attributedLabel An attributed label asking the delegate whether to display the action * sheet. - * @param actionSheet The action sheet that will be displayed if YES is returned. + * @param alertController The alert controller that will be displayed if YES is returned. * @param result The data detector result that was selected. * @param point The point within @c attributedLabel where the result was tapped. - * @returns YES if @c actionSheet should be displayed. NO if @c actionSheet should not be + * @returns YES if @c alertController should be displayed. NO if @c alertController should not be * displayed. */ -- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentActionSheet:(UIActionSheet *)actionSheet withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; +- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentAlertController:(UIAlertController *)alertController withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; +-(UIViewController*)attributedLabel:(NIAttributedLabel *)attributedLabel controllerToPresentAlertController:(UIAlertController *)alertController; #else /** - * Asks the receiver whether an alert controller should be displayed at the given point. + * Asks the receiver whether an action sheet should be displayed at the given point. * - * If this method is not implemented by the receiver then @c alertController will always be displayed if it can. + * If this method is not implemented by the receiver then @c actionSheet will always be displayed. * - * @c alertController will be populated with actions that match the data type that was selected. For + * @c actionSheet will be populated with actions that match the data type that was selected. For * example, a link will have the actions "Open in Safari" and "Copy URL". A phone number will have * @"Call" and "Copy Phone Number". * * @param attributedLabel An attributed label asking the delegate whether to display the action * sheet. - * @param alertController The alert controller that will be displayed if YES is returned. + * @param actionSheet The action sheet that will be displayed if YES is returned. * @param result The data detector result that was selected. * @param point The point within @c attributedLabel where the result was tapped. - * @returns YES if @c alertController should be displayed. NO if @c alertController should not be + * @returns YES if @c actionSheet should be displayed. NO if @c actionSheet should not be * displayed. */ -- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentAlertController:(UIAlertController *)alertController withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; --(UIViewController*)attributedLabel:(NIAttributedLabel *)attributedLabel controllerToPresentAlertController:(UIAlertController *)alertController; +- (BOOL)attributedLabel:(NIAttributedLabel *)attributedLabel shouldPresentActionSheet:(UIActionSheet *)actionSheet withTextCheckingResult:(NSTextCheckingResult *)result atPoint:(CGPoint)point; #endif @end diff --git a/src/NIAttributedLabel.m b/src/NIAttributedLabel.m index cb1769e..0fd1d7c 100644 --- a/src/NIAttributedLabel.m +++ b/src/NIAttributedLabel.m @@ -111,10 +111,10 @@ - (CGSize)boxSize { @end -#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 -@interface NIAttributedLabel() -#else +#ifdef IF_IOS8_OR_GREATER @interface NIAttributedLabel() +#else +@interface NIAttributedLabel() #endif @property (nonatomic, strong) NSMutableAttributedString* mutableAttributedString; @@ -927,52 +927,7 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self setNeedsDisplay]; } -#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 -- (UIActionSheet *)actionSheetForResult:(NSTextCheckingResult *)result { - UIActionSheet* actionSheet = - [[UIActionSheet alloc] initWithTitle:nil - delegate:self - cancelButtonTitle:nil - destructiveButtonTitle:nil - otherButtonTitles:nil]; - - NSString* title = nil; - if (NSTextCheckingTypeLink == result.resultType) { - if ([result.URL.scheme isEqualToString:@"mailto"]) { - title = result.URL.resourceSpecifier; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Mail", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Email Address", @"")]; - - } else { - title = result.URL.absoluteString; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Safari", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy URL", @"")]; - } - - } else if (NSTextCheckingTypePhoneNumber == result.resultType) { - title = result.phoneNumber; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Call", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Phone Number", @"")]; - - } else if (NSTextCheckingTypeAddress == result.resultType) { - title = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Maps", @"")]; - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Address", @"")]; - - } else { - // This type has not been implemented yet. - NI_DASSERT(NO); - [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy", @"")]; - } - actionSheet.title = title; - - if (!NIIsPad()) { - [actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")]]; - } - - return actionSheet; -} -#else +#ifdef IF_IOS8_OR_GREATER -(UIAlertController *)alertControllerForResult:(NSTextCheckingResult *)result { UIAlertController *alertController = [[UIAlertController alloc] init]; @@ -1053,6 +1008,51 @@ -(UIAlertController *)alertControllerForResult:(NSTextCheckingResult *)result { return alertController; } +#else +- (UIActionSheet *)actionSheetForResult:(NSTextCheckingResult *)result { + UIActionSheet* actionSheet = + [[UIActionSheet alloc] initWithTitle:nil + delegate:self + cancelButtonTitle:nil + destructiveButtonTitle:nil + otherButtonTitles:nil]; + + NSString* title = nil; + if (NSTextCheckingTypeLink == result.resultType) { + if ([result.URL.scheme isEqualToString:@"mailto"]) { + title = result.URL.resourceSpecifier; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Mail", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Email Address", @"")]; + + } else { + title = result.URL.absoluteString; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Safari", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy URL", @"")]; + } + + } else if (NSTextCheckingTypePhoneNumber == result.resultType) { + title = result.phoneNumber; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Call", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Phone Number", @"")]; + + } else if (NSTextCheckingTypeAddress == result.resultType) { + title = [self.mutableAttributedString.string substringWithRange:self.actionSheetLink.range]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Open in Maps", @"")]; + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy Address", @"")]; + + } else { + // This type has not been implemented yet. + NI_DASSERT(NO); + [actionSheet addButtonWithTitle:NSLocalizedString(@"Copy", @"")]; + } + actionSheet.title = title; + + if (!NIIsPad()) { + [actionSheet setCancelButtonIndex:[actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")]]; + } + + return actionSheet; +} #endif - (void)_longPressTimerDidFire:(NSTimer *)timer { @@ -1061,7 +1061,24 @@ - (void)_longPressTimerDidFire:(NSTimer *)timer { if (nil != self.touchedLink) { self.actionSheetLink = self.touchedLink; -#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 +#ifdef IF_IOS8_OR_GREATER + UIAlertController *alertController = [self alertControllerForResult:self.actionSheetLink]; + + BOOL shouldPresent = YES; + if ([self.delegate respondsToSelector:@selector(attributedLabel:shouldPresentAlertController:withTextCheckingResult:atPoint:)]) { + shouldPresent = [self.delegate attributedLabel:self shouldPresentAlertController:alertController withTextCheckingResult:self.touchedLink atPoint:self.touchPoint]; + } + + if (shouldPresent) { + UIViewController *vc = nil; + if ([self.delegate respondsToSelector:@selector(attributedLabel:controllerToPresentAlertController:)]){ + vc = [self.delegate attributedLabel:self controllerToPresentAlertController:alertController]; + } + [vc presentViewController:alertController animated:true completion:nil]; + }else{ + self.actionSheetLink = nil; + } +#else UIActionSheet* actionSheet = [self actionSheetForResult:self.actionSheetLink]; BOOL shouldPresent = YES; @@ -1080,23 +1097,6 @@ - (void)_longPressTimerDidFire:(NSTimer *)timer { } else { self.actionSheetLink = nil; } -#else - UIAlertController *alertController = [self alertControllerForResult:self.actionSheetLink]; - - BOOL shouldPresent = YES; - if ([self.delegate respondsToSelector:@selector(attributedLabel:shouldPresentAlertController:withTextCheckingResult:atPoint:)]) { - shouldPresent = [self.delegate attributedLabel:self shouldPresentAlertController:alertController withTextCheckingResult:self.touchedLink atPoint:self.touchPoint]; - } - - if (shouldPresent) { - UIViewController *vc = nil; - if ([self.delegate respondsToSelector:@selector(attributedLabel:controllerToPresentAlertController:)]){ - vc = [self.delegate attributedLabel:self controllerToPresentAlertController:alertController]; - } - [vc presentViewController:alertController animated:true completion:nil]; - }else{ - self.actionSheetLink = nil; - } #endif } } @@ -1515,7 +1515,8 @@ - (NSInteger)indexOfAccessibilityElement:(id)element { #pragma mark - UIActionSheetDelegate -#if __IPHONE_OS_VERSION_MIN_REQUIRED < 8000 +#ifdef IF_IOS8_OR_GREATER +#else - (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (NSTextCheckingTypeLink == self.actionSheetLink.resultType) { if (buttonIndex == 0) { @@ -1524,7 +1525,7 @@ - (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger) } else if (buttonIndex == 1) { if ([self.actionSheetLink.URL.scheme isEqualToString:@"mailto"]) { [[UIPasteboard generalPasteboard] setString:self.actionSheetLink.URL.resourceSpecifier]; - +  } else { [[UIPasteboard generalPasteboard] setURL:self.actionSheetLink.URL]; }