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

            }
            
            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)
            }
        }
        

//  Created by Michael Loistl on 25/08/2014.
//  Copyright (c) 2014 Michael Loistl. All rights reserved.
//
//
// Fixed by DeMoN, using info from: http://stackoverflow.com/questions/27880650/swift-extract-regex-matches
//

import Foundation
import UIKit

extension String {
    func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
        let from16 = advance(utf16.startIndex, nsRange.location, utf16.endIndex)
        let to16 = advance(from16, nsRange.length, utf16.endIndex)
        if let from = String.Index(from16, within: self),
            let to = String.Index(to16, within: self) {
                return from ..< to
        }
        return nil
    }
    
    func NSRangeFromRange(range : Range<String.Index>) -> NSRange {
        let utf16view = self.utf16
        let from = String.UTF16View.Index(range.startIndex, within: utf16view)
        let to = String.UTF16View.Index(range.endIndex, within: utf16view)
        return NSMakeRange(from - utf16view.startIndex, to - from)
    }
}

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