【问题标题】:Xcode-style placeholders in an NSTextViewNSTextView 中的 Xcode 样式占位符
【发布时间】:2014-07-24 10:31:55
【问题描述】:

在 Xcode 中,如果您在文本编辑器中键入 <# Hello, Word #>,它会自动转换为淡蓝色药丸形状的占位符,但在磁盘上,文本仍与键入时完全相同。有谁知道使用NSTextView 是否可以达到相同的效果?我有一些非常丑陋的文件路径,它们必须保持原样,以便sphinx 可以将我的文档放在一起,但是当用户在我的自定义文本编辑器中查看文件时,我想向用户展示一些更有吸引力的东西。

// This on disk (and in any other text editor)
.. image:: images/ssafs/sdfd-sdfsdg-ewfsdf.png

// This shown to the user in my custom text editor
Image of a golden eagle

【问题讨论】:

  • 图片是可点击的还是只是彩色的?
  • 理想情况下,我猜它是可点击的(可能显示被引用的 .png 的快速查看),但关键方面是,无论用户看到什么,文件系统上的文件都包含实际链接。
  • 我已经检查了答案并接受了它。不过,我应该为没有及时回复而道歉——我没有意识到赏金是有时间限制的,所以我认为你只能得到 25 分。已经联系了 SO,看看他们是否会给你一点余地,这样你就可以获得全部 50 个。
  • 不,一切都很好,只是我想知道它是否有效。老实说,我无意要求接受或其他什么。

标签: xcode cocoa nstextview


【解决方案1】:

尽可能尝试将解释写成代码中的注释。我在这里做了什么。

  1. 使用正则表达式找到所有匹配的链接.. image:: images/ssafs/sdfd-sdfsdg-ewfsdf1.png,并将它们添加到数组中。
  2. 将所有匹配的链接.. image:: images/ssafs/sdfd-sdfsdg-ewfsdf1.png替换为[Image]字符串
  3. 找到所有 [Image] 字符串并使用 NSMutableAttributedString 作为链接进行格式化。

它会按照您的要求执行,并且会即时执行,您在数据库/文件中的源根本不会改变。

.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSTextViewDelegate>
@property (unsafe_unretained) IBOutlet NSTextView *aTextView;

.m

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    
    //Your NSTextView
    [aTextView setDelegate:(id)self];
    
    // The Context
    NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec convallis .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf1.png lacinia diam, in mattis quam egestas in. Nam gravida dolor adipiscing velit faucibus, vulputate facilisis diam facilisis. Duis id magna nibh. Proin sed turpis aliquet .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf2.png, posuere purus eget, condimentum nulla. Aenean erat odio, suscipit eu aliquet eget, porta in justo. Quisque sed sem dignissim, luctus .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf3.png libero ut, congue libero. Curabitur tristique fermentum risus in fermentum.";
    
    //Regex to find your links .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf2.png
    //You can / should improve Reges patter.
    NSRegularExpression *regexPatternForFullLinks = [NSRegularExpression regularExpressionWithPattern:@"(\\.\\.\\s(.*?\\.png))"
                                                                                              options:NSRegularExpressionCaseInsensitive error:nil];
    //Here find all image links and add them into an Array
    NSArray *arrayOfAllMatches = [regexPatternForFullLinks matchesInString:string options:0 range:NSMakeRange(0, string.length)];
    NSMutableArray *links = [[NSMutableArray alloc] init];
    for (NSTextCheckingResult *match in arrayOfAllMatches) {
        [links addObject:[[string substringWithRange:match.range] stringByReplacingOccurrencesOfString:@".. image:: " withString:@"/"]];
    }

    //Replacing All your links with string: [Image]
    NSString *modifiedString = [regexPatternForFullLinks stringByReplacingMatchesInString:string
                                                                                  options:0
                                                                                    range:NSMakeRange(0, [string length])
                                                                             withTemplate:@"[Image]"];
    
    NSRegularExpression *regexPatternReplaceLinksWithIMAGEStr = [NSRegularExpression regularExpressionWithPattern:@"\\[image\\]"
                                                                                              options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:modifiedString];
    
    //Here,looking for all [Image] strings and add them Link Attribute
    NSArray *arrayOfAllMatchesImageText = [regexPatternReplaceLinksWithIMAGEStr matchesInString:modifiedString
                                                                                        options:0
                                                                                          range:NSMakeRange(0, modifiedString.length)];
   
    for (int i = 0; i < arrayOfAllMatchesImageText.count; i++) {
        NSTextCheckingResult *checkingResult = [arrayOfAllMatchesImageText objectAtIndex:i];
        [attrString beginEditing];
        [attrString addAttribute:NSLinkAttributeName value:[links objectAtIndex:i] range:checkingResult.range];
        [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:checkingResult.range];
        [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:0] range:checkingResult.range];
        [attrString endEditing];
    }
    
    //Set NSTextView Storage text...
    [aTextView.textStorage setAttributedString:attrString];
}

NSTextViewDelegate:clickedOnLink 处理链接点击。

//Open Given Links with Preview App - NSTextViewDelegate 
- (BOOL)textView:(NSTextView *)aTextView clickedOnLink:(id)link atIndex:(NSUInteger)charIndex {
    [[NSWorkspace sharedWorkspace] openFile:link withApplication:@"Preview"];
    NSLog(@"%@", link);
    return YES;
}

您可以在图像上看到最终结果。如果您愿意,您也可以为链接提供背景颜色。

NSTextView 设置也很重要。

更新:

只要弄清楚这可以做得更优雅,而且效率更高(更快)。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    //Your NSTextView
    [aTextView setDelegate:(id)self];
    
    // The Context
    NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec convallis .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf1.png lacinia diam, in mattis quam egestas in. Nam gravida dolor adipiscing velit faucibus, vulputate facilisis diam facilisis. Duis id magna nibh. Proin sed turpis aliquet .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf2.png, posuere purus eget, condimentum nulla. Aenean erat odio, suscipit eu aliquet eget, porta in justo. Quisque sed sem dignissim, luctus .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf3.png libero ut, congue libero. Curabitur tristique fermentum risus in fermentum.";
    
    //Regex to find your links .. image:: images/ssafs/sdfd-sdfsdg-ewfsdf2.png
    //You can / should improve Reges patter.
    NSRegularExpression *regexPatternForFullLinks = [NSRegularExpression regularExpressionWithPattern:@"(\\.\\.\\s(.*?\\.png))"
                                                                                              options:NSRegularExpressionCaseInsensitive error:nil];
    //Here find all image links and add them into an Array
    NSArray *arrayOfAllMatches = [regexPatternForFullLinks matchesInString:string options:0 range:NSMakeRange(0, string.length)];
    
    __block NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:string];

    [arrayOfAllMatches enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSTextCheckingResult *match = [arrayOfAllMatches objectAtIndex:idx];
        NSString *linkValue = [[string substringWithRange:match.range] stringByReplacingOccurrencesOfString:@".. image:: " withString:@"/"];
        NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [NSColor greenColor],
                                         NSUnderlineStyleAttributeName: [NSNumber numberWithInt:0],
                                         NSLinkAttributeName:linkValue};
        
        NSMutableAttributedString *tempAttrString = [[NSMutableAttributedString alloc] initWithString:@"[Image]" attributes:linkAttributes];
        [attrString beginEditing];
        [attrString replaceCharactersInRange:match.range withAttributedString:tempAttrString];
        [attrString endEditing];
    }];
    [aTextView.textStorage setAttributedString:attrString];
}

【讨论】:

    【解决方案2】:

    我认为您可以使用NSTextAttachmentNSTextAttachmentCell 的自定义子类来做这种事情。

    基本上用你的和NSTextAtachment创建一个NSAttributedString,它使用你的自定义NSTextAttachmentCell

    源代码示例:

    来自BGHUDAppKit framework

    还有一个 extension of that class 用于从 e.printstacktrace() 进行着色

    编辑:看起来 BGHUD 的 NSTokenAttachmentCell 实际上是一个私有苹果类。因此,如果您不需要与 AppStore 兼容,则可以直接使用它。

    【讨论】:

      猜你喜欢
      • 2020-02-26
      • 1970-01-01
      • 2013-08-26
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多