Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make #key and @key clickable in IOS7

Tags:

ios

ios6

ios5

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

enter image description here

like image 931
nightograph Avatar asked Feb 15 '23 00:02

nightograph


1 Answers

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.

like image 111
Brian Nickel Avatar answered Feb 27 '23 01:02

Brian Nickel