             for match in matches {
                 if let textCheckingResult = match as? NSTextCheckingResult {
                     let matchRange = textCheckingResult.range
-                    let stringIndexRange = advance(text.startIndex, matchRange.location)..<advance(text.startIndex, matchRange.location + matchRange.length)
-
+                    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(advance(text.startIndex, matchRange.location)..<advance(text.startIndex, matchRange.location + matchRange.length))
+                        realURL = plainText.substringWithRange(stringIndexRange!)
                     }
 
                     if textCheckingResult.resultType == .Link {
                         if let matchString = realURL as? String {
-                            rangesForURLs.append(LinkRangeResult(linkDetectionType: LinkDetectionType.URL, linkRange: stringIndexRange, linkString: matchString, textLink: nil))
+                            rangesForURLs.append(LinkRangeResult(linkDetectionType: LinkDetectionType.URL, linkRange: stringIndexRange!, linkNSRange: matchRange, linkString: matchString, textLink: nil))
                         }
                     }
                 }

//  Created by Michael Loistl on 25/08/2014.
//  Copyright (c) 2014 Michael Loistl. All rights reserved.
//

import Foundation
import UIKit

protocol ContextLabelDelegate {
    func contextLabel(contextLabel: ContextLabel, beganTouchOf text: String, with linkRangeResult: LinkRangeResult)

            }
            
            if attributes != nil {
                mutableAttributedString.setAttributes(attributes!, range: linkRangeResult.linkNSRange)
            }
        }
        
