             }
             
             if attributes != nil {
-                let location = distance(text.startIndex, linkRangeResult.linkRange.startIndex)
-                let length = distance(linkRangeResult.linkRange.startIndex, linkRangeResult.linkRange.endIndex)
-                let range = NSMakeRange(location, length)
-                
-                mutableAttributedString.setAttributes(attributes!, range: range)
+                mutableAttributedString.setAttributes(attributes!, range: linkRangeResult.linkNSRange)
             }
         }
         

public class LinkRangeResult: NSObject {
    var linkDetectionType: ContextLabel.LinkDetectionType
    var linkRange: Range<String.Index>
    var linkString: String
    var textLink: ContextLabel.TextLink?
    
    // MARK: Initializers
    
    init(linkDetectionType: ContextLabel.LinkDetectionType, linkRange: Range<String.Index>, linkString: String, textLink: ContextLabel.TextLink?) {
        self.linkDetectionType = linkDetectionType
        self.linkRange = linkRange
        self.linkString = linkString
        self.textLink = textLink
        

            for match in matches {
                if let textCheckingResult = match as? NSTextCheckingResult {
                    let matchRange = textCheckingResult.range
                    let stringIndexRange = text.rangeFromNSRange(matchRange)
                    // 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(stringIndexRange!)
                    }

                    if textCheckingResult.resultType == .Link {
                        if let matchString = realURL as? String {
                            rangesForURLs.append(LinkRangeResult(linkDetectionType: LinkDetectionType.URL, linkRange: stringIndexRange!, linkNSRange: matchRange, linkString: matchString, textLink: nil))
                        }
                    }
                }
