Anyone knows how I can make the #KEY and @NAME clickable in the text of comments in IOS7 (the same way that instagram does it for example)? i am trying to use NSMutableAttributedString but I'm not sure how to detect click event, in the image below clicking @Username should take the user to the profile of the user
Looking at the UITextViewDelegate
protocol, there is a new method in iOS7: textView:shouldInteractWithURL:inRange:
.
You didn't share any code, but it is safe to assume you have an attributedString
and a range
representing the area you turned blue. I'll also assume you can extract the username into a variable called username
.
With these three pieces of information, you add a link attribute to that range.
[attributedString addAttribute:NSLinkAttributeName
value:[@"username://" stringByAppendingString:username]
range:range];
In your delegate, you could do something like this:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
if ([URL.scheme isEqualToString:@"username"]) {
[self doSomethingWithUserName:URL.host];
return NO;
}
return YES;
}
I believe they demoed this in the Introducing Text Kit session at WWDC 2013.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With