Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KILabel.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|

s.name = "KILabel"
s.version = "1.0.1"
s.version = "1.0.2"
s.summary = "Replacement for UILabel for iOS 7 and 8 that provides automatic detection of links such as URLs, twitter style usernames and hashtags."

s.description = <<-DESC
Expand Down
30 changes: 28 additions & 2 deletions KILabel/Source/KILabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,12 @@ - (NSArray *)getRangesForURLs:(NSAttributedString *)text
NSRange matchRange = [match range];

// If there's a link embedded in the attributes, use that instead of the raw text
NSString *realURL = [text attribute:NSLinkAttributeName atIndex:matchRange.location effectiveRange:nil];
id resultObject = [text attribute:NSLinkAttributeName atIndex:matchRange.location effectiveRange:nil];
NSString *realURL = resultObject;
if ([resultObject respondsToSelector: @selector(absoluteString)])
{
realURL = [resultObject absoluteString];
}
if (realURL == nil)
realURL = [plainText substringWithRange:matchRange];

Expand All @@ -493,7 +498,28 @@ - (NSArray *)getRangesForURLs:(NSAttributedString *)text
}
}
}


[text enumerateAttribute:NSLinkAttributeName inRange:NSMakeRange(0, text.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
if (value != nil)
{
if ([value class] == [NSURL class])
{
NSURL *valueURL = (NSURL *) value;
[rangesForURLs addObject:@{KILabelLinkTypeKey : @(KILinkTypeURL),
KILabelRangeKey : [NSValue valueWithRange:range],
KILabelLinkKey : valueURL.absoluteString,
}];
}
else
{
[rangesForURLs addObject:@{KILabelLinkTypeKey : @(KILinkTypeURL),
KILabelRangeKey : [NSValue valueWithRange:range],
KILabelLinkKey : value,
}];
}
}
}];

return rangesForURLs;
}

Expand Down
5 changes: 5 additions & 0 deletions KILabelDemo/KILabelDemo/KIViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ - (void)viewDidLoad

_label.systemURLStyle = YES;

NSMutableAttributedString *string = [_label.attributedText mutableCopy];
[string addAttribute:NSLinkAttributeName
value:@"https://www.google.com/search?q=interactive"
range:NSMakeRange(11, 11)];
_label.attributedText = string;
// Attach block for handling taps on usenames
_label.userHandleLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSString *message = [NSString stringWithFormat:@"You tapped %@", string];
Expand Down