Last active
November 10, 2017 07:29
-
-
Save NaStillmatic/13dc11befaa6c9fd42a555b5d44ff4b2 to your computer and use it in GitHub Desktop.
[Objective-C] UITextView에 커스텀 링크 연결
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface UITextView (CustomLink) | |
-(void)setCustomLink:(NSString*)string fontSize:(CGFloat)fontSize linkTextColor:(UIColor*)linkTextColor linkUnderLineColor:(UIColor*)linkUnderLineColor stringArray:(NSArray*)stringArray URLArray:(NSArray*)URLArray; | |
@end |
This file contains hidden or 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
#import <CoreText/CoreText.h> | |
@implementation UITextView (CustomLink) | |
-(void)setCustomLink:(NSString*)string fontSize:(CGFloat)fontSize linkTextColor:(UIColor*)linkTextColor linkUnderLineColor:(UIColor*)linkUnderLineColor stringArray:(NSArray*)stringArray URLArray:(NSArray*)URLArray | |
{ | |
if (stringArray.count != URLArray.count) | |
{ | |
return; | |
}else{ | |
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:fontSize]}]; | |
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: linkTextColor, | |
NSUnderlineColorAttributeName: linkUnderLineColor, | |
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)}; | |
self.linkTextAttributes = linkAttributes; | |
for (int i = 0; i < stringArray.count; ++i) | |
{ | |
NSRange range = [string rangeOfString:[stringArray objectAtIndex:i]]; | |
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName | |
value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] | |
range:range]; | |
NSURL *URL = [NSURL URLWithString:[URLArray objectAtIndex:i]]; | |
[attString addAttribute:NSLinkAttributeName value:URL range:range]; | |
} | |
self.attributedText = attString; | |
} | |
} | |
@end |
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@property (weak, nonatomic) IBOutlet UITextView *textView; | |
@end | |
This file contains hidden or 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
#import "UITextView+CustomLink.h" | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSString *strAgreement = @"서비스약관, 유료이용약관, 개인정보 취급방침에 동의합니다."; | |
NSArray *textArray =@[@"서비스약관", @"유료이용약관", @"개인정보 취급방침"]; | |
NSArray *urlArray = @[@"agreement://0", @"agreement://1", @"agreement://2"]; | |
[self.textView setCustomLink:strAgreement fontSize:14 linkTextColor:[UIColor redColor] linkUnderLineColor:[UIColor redColor] stringArray:textArray URLArray:urlArray]; | |
} | |
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange | |
{ | |
if ([URL.scheme isEqualToString:@"agreement"]) | |
{ | |
NSLog(@"select Link = %ld", URL.host.integerValue); | |
return NO; | |
} | |
return YES; | |
} |
Author
NaStillmatic
commented
Nov 10, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment