-
-
Save heliang219/82fc92656c86b9f84dc687ea14b37651 to your computer and use it in GitHub Desktop.
Make a clickable link in an UILabel
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
self.policyLabel.html = @"Please <a href='https://gist.github.com/yycking/aef2cb0afafbaf3a04a4e40123704437'>clcik me</a>. "; |
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
@implementation UILabel (PLUS) | |
- (void)setHtml:(NSString*)text { | |
NSString *color = [self hexFromUIColor:self.textColor]; | |
NSString *html = [NSString stringWithFormat:@"<html><head><style type=\"text/css\">\ | |
body {font-family: '%@'; font-size:%@px;color: %@;}\ | |
a {color: %@;}\ | |
</style></head><body>%@</body></html>", | |
self.font.fontName, @(self.font.pointSize), color, | |
color, | |
text]; | |
self.attributedText = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] | |
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, | |
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} | |
documentAttributes:nil error:nil]; | |
self.userInteractionEnabled = YES; | |
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnLabel:)]]; | |
} | |
- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGesture | |
{ | |
NSAttributedString *text = self.attributedText; | |
NSRange range = NSMakeRange(0, text.length); | |
CGPoint touch = [tapGesture locationInView:self]; | |
NSLayoutManager *layoutManager = NSLayoutManager.new; | |
NSTextStorage *textStorage = [NSTextStorage.alloc initWithAttributedString:text]; | |
[textStorage addLayoutManager:layoutManager]; | |
NSTextContainer *textContainer = [NSTextContainer.alloc initWithSize:self.bounds.size]; | |
textContainer.lineFragmentPadding = 0; | |
textContainer.maximumNumberOfLines = self.numberOfLines; | |
textContainer.lineBreakMode = self.lineBreakMode; | |
[layoutManager addTextContainer:textContainer]; | |
[layoutManager ensureLayoutForGlyphRange:range]; | |
CGRect textBoundingBox = [layoutManager usedRectForTextContainer:textContainer]; | |
CGPoint textContainerOffset = CGPointMake( | |
(self.bounds.size.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, | |
(self.bounds.size.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y); | |
CGPoint locationOfTouchInTextContainer = CGPointMake(touch.x - textContainerOffset.x, | |
touch.y - textContainerOffset.y); | |
NSInteger characterIndex = [layoutManager characterIndexForPoint:locationOfTouchInTextContainer inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil]; | |
NSDictionary *attrs = [text attributesAtIndex:characterIndex effectiveRange:&range]; | |
NSString *url = attrs[NSLinkAttributeName]; | |
if (url) { | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment