Search code examples
iosobjective-cuilabelnsattributedstringtttattributedlabel

How to reduce length of links OR set a fixed length for links detected in TTTAttributedLabel


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.


Solution

  • 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;
    }