4

如何使用包含 2 列 NSTextTable 的 NSAttributedString 在 NSTextView 上进行拖动选择,仅选择第一列中的文本?

IE

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

单击拖动时,不选择时间,仅选择对话。您可以在此处看到 Skype 执行此操作:

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

更新

我认为Skype正在使用WebView和CSS:

-webkit-user-select: none

接着

-webkit-user-select: text

对于可选择的部分。

4

3 回答 3

0

您可以创建多个表格,每个表格有一列,并安排它们以给人一种单一表格的感觉。

这将允许选择表中的单个列。

于 2013-11-21T07:52:22.547 回答
0

不知道如何使用 NSTextView 解决这个问题,但我找到了一个使用 WebView + HTML5 + canvas + javascript 的解决方案

Names are created as canvas objects and text is just normal text in html:

When text is selected, it looks like this:

And after copy+paste you get only conversation as result:


I used canvas because using "-webkit-user-select: none;" in div, text will be copied if you select text around that div. And javascript is needed to create new canvas dynamically.

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

Calling javascript function from objective-c:

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

So this solution is very easy to implement.

于 2013-11-21T14:02:10.607 回答
0

Here is something that may help you in the right direction, it will only allow selection of text in between your two delimeters '[' and '|'. The only thing is once a line is partially selected it will select the whole line. This can be changed if needed by using the overlap value. I have not fully tested it as I have yet to create the .xib to match your set up.

- (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;
}
于 2013-11-21T18:06:26.940 回答