在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
答案 0 :(得分:4)
尽可能尝试将解释写为代码中的注释。我在这里做了什么。
.. image:: images/ssafs/sdfd-sdfsdg-ewfsdf1.png
和Regex,并将它们添加到数组中。.. image:: images/ssafs/sdfd-sdfsdg-ewfsdf1.png
替换为 [图片] 字符串它会按照您的要求进行操作,而且它会动态执行,您在数据库/文件中的来源根本不会发生变化。
.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];
}
答案 1 :(得分:0)
我认为您可以使用NSTextAttachment
和NSTextAttachmentCell
的自定义子类来执行此类操作。
基本上使用您使用自定义NSAttributedString
的{{1}}创建NSTextAtachment
源代码示例:
来自BGHUDAppKit framework 的来自e.printstacktrace()
的 修改:看起来BGHUD的NSTextAttachmentCell
实际上是私人苹果类。所以你可以直接使用它,如果你不需要AppStore兼容。