views:

216

answers:

2

I want to add a data detector in an NSTextField / NSTextView like in Mail (e.g. date, email, URL, etc).

Does anyone have an idea how to do this ? I think I saw it in TextEdit's source code but when I checked it was 2005's version and I can't find the new source code.

Alex

+2  A: 

You need to set the formatter outlet of the object. Built in formatters are NSDateFormatter and NSNumberFormatter.

alt text

You can define custom formatters by extending the NSFormatter class. There is a chapter in Aaron Hillegass excellent 'Cocoa Programming for Max OS X' on Creating NSFormatters.

To detect dates, url, email address, etc. within the body of some text, you can use the Latent Semantic Mapping Framework. It's a public API as of Leopard but there isn't a great deal of documentation for it. There is also a command line tool called lsm that would allow you to test any custom extractors you want to use. It is a C API and not integrated into Cocoa as far as I know so you'd need to wire it up yourself

Robert Christie
thanks for the answer. But how do I use NSFormatter to detect data in text ? for example if I have a text that goes 'Hey how are you ? Let's meet sunday. My number is 123-555-000'. If I received this in Mail and mouse-overed 'sunday' it would propose me to ad the event to iCal, and if I mouse-overed '123-555-000' it would propose me to add it to a contact or something. How do I achieve this behaviour?
Alexandre Cassagne
@Alexandre Cassagne: Ah - OK. Have added a paragraph on the Latent Semantic Mapping framework which I think is what you want.
Robert Christie
It's gonna be tough to work with, but it's what I'm looking for.Thanks!
Alexandre Cassagne
+1  A: 

In Snow Leopard you can now enable Data Detectors in a NSTextView by simply clicking the appropriate checkbox in the IB inspector.

Thomas Deniau
Thanks – much simpler than the above XD
Alexandre Cassagne