         if characterIndex <= textStorage?.length {
             if let linkRangeResults = contextLabelData?.linkRangeResults {
                 for linkRangeResult in linkRangeResults {
-                    let rangeLocation = text.startIndex.distanceTo(linkRangeResult.linkRange.startIndex)
-                    let rangeLength = linkRangeResult.linkRange.startIndex.distanceTo(linkRangeResult.linkRange.endIndex)
+                    let rangeLocation = distance(text.startIndex, linkRangeResult.linkRange.startIndex)
+                    let rangeLength = distance(linkRangeResult.linkRange.startIndex, linkRangeResult.linkRange.endIndex)
                     
                     if rangeLocation <= characterIndex &&
                         (rangeLocation + rangeLength - 1) >= characterIndex {


        // Use a data detector to find urls in the text
        let plainText = attributedString.string
        do {
            let dataDetector = try NSDataDetector(types: NSTextCheckingType.Link.rawValue)
            let matches = dataDetector.matchesInString(plainText, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, plainText.characters.count))

            // Add a range entry for every url we found
            for match in matches {
                let matchRange = match.range
                let stringIndexRange = text.startIndex.advancedBy(matchRange.location)..<text.startIndex.advancedBy(matchRange.location + matchRange.length)
                
                // If there's a link embedded in the attributes, use that instead of the raw text
                var realURL: AnyObject? = attributedString.attribute(NSLinkAttributeName, atIndex: matchRange.location, effectiveRange: nil)
                if realURL == nil {
                    realURL = plainText.substringWithRange(text.startIndex.advancedBy(matchRange.location)..<text.startIndex.advancedBy(matchRange.location + matchRange.length))
                }
                
                if match.resultType == .Link {
                    if let matchString = realURL as? String {
                        rangesForURLs.append(LinkRangeResult(linkDetectionType: LinkDetectionType.URL, linkRange: stringIndexRange, linkString: matchString, textLink: nil))
                    }
                }
            }
        } catch _ as NSError {
            
        }

        return rangesForURLs
    }
    
    private func addLinkAttributesToAttributedString(attributedString: NSAttributedString, withLinkRangeResults linkRangeResults: [LinkRangeResult], highlighted: Bool = false) -> NSAttributedString {
        let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
        
        for linkRangeResult in linkRangeResults {
            var attributes: [String: AnyObject]?
            
            if linkRangeResult.linkDetectionType == .TextLink {
                let color = highlighted ? privateTextLinkHighlightedTextColor : textLinkTextColor

        super.touchesMoved(touches, withEvent: event)
    }
    
    public override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        
        addLinkAttributesToLinkRangeResultWithTouches(touches, highlighted: false)
        
