2

The links pasted by users are very long and contain "http://" etc and hence I want to restrict the length of links and show only the main website name or a bit more than that.

Example:

Link pasted by user:

http://www.androidpolice.com/2015/08/16/an-alleged-leak-of-lgs-nexus-5-2015-bullhead-pops-up-on-google/

Link I want to display in the label: www.androidpolice.com/2015/08/...

Is there any way to do it?

I searched and found something called attributedTruncationToken but I didn't understand much and I think it's related to truncation at the end of line.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Vineet Ashtekar
  • 1,952
  • 21
  • 16
  • this just seems like an NSAttributedString by. Learn how to use them and just mask it with whatever text you want for the range you desire for the attributes. Tutorials are everywhere. You will have to scan for substrings so you ensure the first substrings are included http:// + www + url + name + .com +/%@+/%@ and then just rename it for that range – soulshined Aug 17 '15 at 14:18

1 Answers1

0

I don't use TTTAttributeLabel but this answer is for all the future question seekers that are struggling to create a URL shortener without 3rd party API's.

Simply use a NSMutableAttributedString and pass into a UIDataDetectorTypeLink capable object:

Let's say your user inputs a link or passes a string entirely:

I am a user. I want to enter this URL to share to all other awesome users of Apple products, and only Apple products :). Check out my link here http://www.androidpolice.com/2015/08/16/an-alleged-leak-of-lgs-nexus-5-2015-bullhead-pops-up-on-google/

We can easily extract the text using commonly frequented methods:

NSString *userInput = @"I am a user. I want to enter this URL to share to all other awesome users of Apple products, and only Apple products :). Check out my link here http://www.androidpolice.com/2015/08/16/an-alleged-leak-of-lgs-nexus-5-2015-bullhead-pops-up-on-google/"

// get the range of the substring (url) starting with @"http://"
NSRange httpRange = [userInput rangeOfString:@"http://" options:NSCaseInsensitiveSearch];

if (httpRange.location == NSNotFound) {
    NSLog(@"URL not found");
} else {
    //Get the new string from the range we just found
    NSString *urlString = [userInput substringFromIndex:httpRange.location];
    //create the new url
    NSURL *newURL = [NSURL URLWithString:urlString];
    //cut off the last components of the string
    NSString *shortenedName = [urlString stringByDeletingLastPathComponent];
    //new output
    NSLog(@"%@", [NSString stringWithFormat:@"\n original user input : \n %@ \n original URL name : \n %@ \n shortenedName : \n %@", userInput, urlString, shortenedName]);

    //We have everything we need so all we have remaining to do is create the attributes and pass into a UITextView. 
    self.someTextView.attributedText = [self createHyperLinkForURL:newURL withName:shortenedName];
}

Where [self createHyperLinkForURL:newURL withName:shortenedName];

-(NSMutableAttributedString *)createHyperLinkForURL:(NSURL *)url withName:(NSString *)hyperLinkText {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:hyperLinkText];
    [string beginEditing];
    [string addAttribute:NSLinkAttributeName
               value:url
               range:NSMakeRange(0, string.length)];
    [string endEditing];
    return string;
}
soulshined
  • 9,612
  • 5
  • 44
  • 79
  • What exactly this method does - stringByDeletingLastPathComponent ? In the Apple Documentation it says we should not use this with web URLs. – Vineet Ashtekar Aug 18 '15 at 04:33
  • Exactly as it says : deletes the last path of the string you provide. So within a path that has `/path/subpath/subpath/subpath` it removes the last one, no matter how many there are. Just run it through the console log one time and you'll see. Everything you need is provided in the answer – soulshined Aug 18 '15 at 04:35
  • Oh and @VineetAshtekar your not using it with a weburl your technically using it with a string `NSString *urlString` newURL is the actual url. – soulshined Aug 18 '15 at 05:48