-(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中创建应用内链接所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)