Is it possible to select drag and drop in NSTextView (using NSTextTable) only one column?

How to make drag and drop selection in NSTextView using NSAttributedString which contains 2 NSTextTable columns, select only text in 1st column?

i.e.

    [hi | 10:00 AM]
    [hello | 10:01 AM]
    [what are you doing? | 10:02 AM]
    [nothing, you? | 10:03 AM]

When you click and drag, time is not selected, but just a conversation. You can see how skype does it here:

https://dl.dropboxusercontent.com/u/2510380/skype.mov

Update

I think skype uses WebView and CSS:

-webkit-user-select: none

and then

-webkit-user-select: text

for parts that can be selected.

+4
source share
3 answers

, NSTextView, WebView + HTML5 + canvas + javascript

canvas, - html:

, :

+ :


, "-webkit-user-select: none;" div , div. javascript.

var canvas = document.createElement("canvas");
canvas.width = 400;
canvas.height = 20;
canvas.setAttribute("style", "-webkit-user-select: none;");

var context=canvas.getContext("2d");
context.font="15px Arial";
context.fillText(text,0,20);

javascript objective-c:

[_webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];

, .

0

, .

.

0

Here is something that can help you in the right direction, it will only allow you to select the text between your two separators "[" and "|". The only thing is that when the line is partially selected, it will select the entire line. If necessary, this can be changed using the overlap value. I have not fully tested it, since I still need to create .xib so that it matches your setup.

- (NSArray *)textView:(NSTextView *)aTextView willChangeSelectionFromCharacterRanges:(NSArray *)oldSelectedCharRanges toCharacterRanges:(NSArray *)newSelectedCharRanges
{
    NSMutableArray *newRanges = [[NSMutableArray alloc] init];
    NSString *fullText = aTextView.string;
    //Regex to find text between [ and |, the only text we should highlight
    NSString *pattern = @"\[(.*?)\|";
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

    NSRange range = NSMakeRange(0,[fullText length]);
    [expression enumerateMatchesInString:fullText options:0 range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
        {
        NSRange matchedRange = [result rangeAtIndex:1];
        //Loop through all ranges to see if they contain any allowable text
        for (int i=0; i<newSelectedCharRanges.count; i++)
        {
            NSRange rangeToCheck = [[newSelectedCharRanges objectAtIndex:i] rangeValue];
            NSRange overlap = NSIntersectionRange(rangeToCheck, matchedRange);
            if (overlap.length > 0)
            {
                //If text has been partially selected, select whole allowable range
                [newRanges addObject:[NSValue valueWithRange:matchedRange]];
            }
        }
    }];
    return newRanges;
}
0
source

All Articles