// 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 editorImage of a golden eagle解决方法 尽可能多地尝试将解释写为代码中的注释.我在这里做了什么.
>找到所有匹配的链接.. image :: images / ssafs / sdfd-sdfsdg-ewfsdf1.png与Regex并将它们添加到一个数组中.
>用[Image]字符串替换所有匹配的链接.. image :: images / ssafs / sdfd-sdfsdg-ewfsdf1.png
>找到所有[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,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];}总结
以上是内存溢出为你收集整理的NSTextView中的Xcode样式占位符全部内容,希望文章能够帮你解决NSTextView中的Xcode样式占位符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)