cocoa – 在NSTextView中创建应用内链接

cocoa – 在NSTextView中创建应用内链接,第1张

概述我发现这个小片段允许用户在NSTextView中创建文本链接: -(void)setHyperlinkWithTextView:(NSTextView*)inTextView{ // create the attributed string NSMutableAttributedString *string = [[NSMutableAttributedString alloc] 我发现这个小片段允许用户在NSTextVIEw中创建文本链接:

-(voID)setHyperlinkWithTextVIEw:(NSTextVIEw*)inTextVIEw{    // create the attributed string    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];    // create the url and use it for our attributed string    NSURL* url = [NSURL URLWithString: @"http://www.apple.com"];    [string appendAttributedString:[NSAttributedString hyperlinkFromString:@"Apple Computer" withURL:url]];    // apply it to the NSTextVIEw's text storage    [[inTextVIEw textStorage] setAttributedString: string];}

是否可以将链接指向我的应用程序中的某些资源,例如,能够解释链接并分派到相应视图/控制器的特定处理程序类?

解决方法 您可以处理NSTextVIEw委托中链接的点击,特别是通过实施 textView:clickedOnLink:atIndex:方法.

如果需要在每个链接中存储更多信息,可以通过将对象存储为字符串的自定义属性并使用以下链接来执行此 *** 作:

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:                            yourObject,@"YourCustomAttributename",@"link",NSlinkAttributename,nil];NSAttributedString* string = [[[NSAttributedString alloc] initWithString:@"Your string" attributes:attributes] autorelease];

确保您是否保存了use the NSCoding protocol的属性字符串而不是NSAttributedString的RTF方法,因为RTF无法存储自定义属性.

总结

以上是内存溢出为你收集整理的cocoa – 在NSTextView中创建应用内链接全部内容,希望文章能够帮你解决cocoa – 在NSTextView中创建应用内链接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存