Cocoa:创建自定义文本视图 [英] Cocoa: Creating a Custom Text View

查看:157
本文介绍了Cocoa:创建自定义文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我不想要原始文本,甚至是富文本。我想加载一个xml文档,其中包含文本部分的元数据,当我点击给定的文本部分时,我想在抽屉中显示该元数据。超链接是一个很好的例子;在Web应用程序中显然很微不足道,但是虽然我不是那种经验丰富的mac开发者,我似乎找不到一个简单的方法来实现可可。



<对总体策略有任何建议吗?

解决方案

I'不完全清楚你想要做什么。



如果您要加载XML文档,显示文本并显示各种元数据,则会发出在这种情况下,您应该阅读可可文本系统 NSTextStorage 类是 NSMutableAttributedString 的子类,您可以对任何文本范围应用任意属性。当选择更改时,您可以获取所选范围内的属性,并使用该属性更新您的抽屉。 (顺便说一下,抽屉真的在出路,我建议一个不同的用户界面。 NSSplitView 基于界面更加流行这些天。)



当然,要构建 NSTextStorage ,您需要使用 NSXMLDocument NSXMLParser ,但是你会得到更多的控制,它会看起来更多的Cocoa-like。



你可以使用一个 WebView ,这是Safari渲染器,但我想你很难让它显示文本的方式你要。 Safari从来没有伟大的渲染XML没有XSLT。


In a nutshell, I don't want raw text, or even rich text. I want to load an xml document, which has metadata for sections of text, and I want to display that metadata in a drawer when I click on a given text section. A hyperlink is a good example; obviously trivial to do in a web app, but while I'm not that experienced with mac dev, I can't seem to find an easy way to accomplish this with cocoa.

Any suggestions as to general strategy? There doesn't seem to be an HTML view built in to Interface builder or I'd mess with that.

解决方案

I'm not entirely clear on what you're trying to do. It sounds like you want to load an XML document, display the text, and display various metadata when certain bits of text are selected.

If that's the case, you should read about the Cocoa Text System. The NSTextStorage class is a subclass of NSMutableAttributedString, and you can apply arbitrary attributes to any range of text. When the selection changes, you can get the attributes in the selected range and use that to update your drawer. (By the way, drawers are really on their way out. I'd suggest a different user interface. NSSplitView-based interfaces are much more in vogue these days.)

Of course, to build up the NSTextStorage, you'd need to parse the XML with NSXMLDocument or NSXMLParser, but you'd get much more control and it would look more "Cocoa-like".

You could use a WebView, which is the Safari renderer, but I think you'd have a hard time getting it to display text the way you want. Safari has never been great at rendering XML without XSLT.

这篇关于Cocoa:创建自定义文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆