learning Cocoa can be pretty tough for web developers. Some things are so simple in HTML, and I have no idea, how to do this in Cocoa.
Let me just show you an image here, to show you what I have on my mind.
So it's kinda like a blog. Each post has variable length, so it can take up some space. Also, you're able to scroll through posts.
I was thinking about using NSTableView or NSCollectionView, but since I don't know much about Cocoa, I'm asking you for advice.
Also please do link any related articles.
Updates
So here are some things that I discovered.
I could make a subclass of NSCell and use it in Table View. I can use it, I can put there a string, something like this:
http://pastie.org/1140412
(please take a look at this code, I'm wondering if I should use awakeFromNib/setDataCell combination)
But string is not enough. I need a NSTextView. The problem is, it doesn't have method like drawInRect: withAttributes:. So I don't know how to draw it into that cell. I guess I'm missing some basics here, so I'm just gonna study some Cocoa views now.
Any ideas are welcome.
You want to use a NSTableView. And I will recommend to take a look/glance at NSTableView, NSTableViewDelegate and NSTableViewDataSource docs:
http://developer.apple.com/mac/library/documentation/cocoa/reference/ApplicationKit/Classes/NSTableView_Class/Reference/Reference.html
http://developer.apple.com/mac/library/documentation/cocoa/reference/NSTableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40008622
http://developer.apple.com/mac/library/documentation/cocoa/reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40004178
You choose the object you want to be your datasource depending on the data you have.
The delegate is the object that helps you to so some settings, like rows, groups, cells, etc.
There are useful methods of NSTableViewDelegate you want to implement like – tableView:heightOfRow:
and just google NSTableView tutorial or something, there are many good examples ;)
You could just use a WebView and write it in HTML.
Look at http://mattgemmell.com/source for an example: the Skinnable App one.
Related
I'm experimenting with NSCollectionView after doing some practicing with UICollectionView for iOS. I'm planning to write an app which central part is a flexible grid view on that cards can be dragged around freely and I want to start with a OSX version first before tackling a iOS version.
The UICollectionView seemed like a good choice for my purpose so far (with a custom UICollectionViewLayout anyway) but now that I'm getting to know NSCollectionView for Cocoa on OSX this class seems rather meagre since there doesn't seem to be a comparable NSCollectionViewLayout class and in-depth info or tutorials on the web seem really sparsely seeded.
So is the NSCollectionView class actually a recommended choice at all for what I need? Or could anyone recommend me a better approach to get started going the right way with this?
(What I need is a performant, scrollable view with rows and columns onto that sub views can be dragged and dropped around freely.)
I agree, not recommended.
NSCollectionView performance is bad.
NSArrayController is an enigma also.
That said, there was a old example of someone rolling their own,
which is good sample code to peruse.
... google "TigerCollectionView".
Your going to run into issues with every user interaction... drag, drop, insert, deferred layout, reload, etc.
Better to roll your own using arrays of dictionaries, so you can see whats going on.
I am quite new to NSTableView but as I tried to get things straight, I took a look at InterfaceBuilder!!
TableView hierarchy
I understand that objects are responsible for scrolling and clipping. TableView is the real NSTableView instance. The object titled function list ist the NSTableColumn below that is my TableCellView objects. What I don't understand is the object TextCell. It doesn't seem anything to do. Even wrong colors and alike have no effect at all. I am using a view based variant. Is the TextCell solely for cell based TableViews?
I found that part not quite well documented. I am planning on building custom views for my table. So I was thinking a thorough understanding would be a good approach.
Yes, it's strange that that's still there. It's useless and has no effect on anything, but you can't delete it. Ignore it.
I want to know how can I create custom widgets/controls in Cocoa.
Not a full tutorial, but some guidance on what to start looking into. I'm confused by custom views, Core Animation, etc. I feel lost.
I see a lot of cool looking controls, like in Reeder App, or Sparrow etc. For example:
The left side is a collapsable menu that includes animations etc. How can I achieve something similar? I thought of using a WebView + HTML + JavaScript, but that doesn't seem like a very optimized solution.
Controls are views, so if custom views confuse you, you'll need to get that figured out before moving on to custom controls. Although you should really read the entire View Programming Guide, the section called Creating a Custom View will get you started on creating your own views. Try creating a simple view that draws a circle, for example, or the time.
When you've got views figured out, move on to custom controls. Most controls work about the same way. The user touches them, and the control responds by: a) tracking the user's input, b) changing its value, c) sending its action message to its target, and d) giving the user some feedback by redrawing itself. To get started, first make sure that you know how to use controls. Reading Control and Cell Programming Topics should help, and the section titled Subclassing NSControl covers (obviously) creating your own subclasses.
The example you provided is pretty clearly Apple's Mail.app. The view on the left side of the window might be an instance of NSOutlineView, or it might be a custom class. Either way, NSOutlineView would be a good starting point if you want to duplicate that functionality. NSOutlineView is a subclass of NSTableView, which in turn is a subclass of NSControl, which in turn is a subclass of NSView. Read Outline View Programming Topics for help getting started -- tables and outlines are extremely useful, but also more complicated to use than basic controls like buttons and text fields.
I know it's only a part of the UI, but I've recently coded something similar to the sidebar. If you look though the source-code it may give you some help on learning how to use custom controls and cells.
You can check it out on Github:
https://github.com/iluuu1994/ITSidebar
I have been reading and experimenting with allowing links in a custom drawn NSCell for the last few days and have basically got nothing usable, there's always issues with each approach.
Does anyone know of a way of doing this that works?
I am custom drawing the NSCell using - (void)drawInteriorWithFrame:(NSRect)theCellFrame inView:(NSView *)theControlViewm
The NSCell is just a variable height block of text with links inside it, some cells have links, some do not.
I've tried using nsattributedstring with NSLinkAttributeName
I've tried intercepting all hits to the cell and then trying to match up where they clicked to where the link would be in the text but that never works out.
I've basically tried all suggestions that I could find on all boards but most comments are old so I'm hoping someone has figured out a good way to do this.
Thanks, David
I haven't tried this exactly, but give this a try:
First, for hyperlinks I use a category on NSAttributedString, akin to this post from Apple developer docs. The example here gives you a method on NSAttributedString 'hyperlinkFromString:withURL:`
Second, create a delegate for the table, and implement tableView:willDisplayCell:forTableColumn:row: method.
In that method,
setAttributedStringValue:[NSAttributedString hyperlinkFromString:YOUR_STRING withURL:YOUR_URL]
or, if you need non-hyperlinked string text as well,
setAttributedStringValue:[SOME_NON_HYPERLINKED_STRING appendAttributedString:[NSAttributedString hyperlinkFromString:YOUR_STRING withURL:YOUR_URL]]
If that is the only reason you are custom drawing an NSCell, you can try getting rid of your custom class, because this should work with an NSTextFieldCell. I've seen online, though, that some people have had trouble with centering attributed strings in text field cells, so I hope it works ok. One other caveat: with the delegate method, be sure that you set the cell attribute that you are changing for all conditions. I quote:
Because aCell is reused for every row in aTableColumn, the delegate must set the display attributes both when drawing special cells and when drawing normal cells.
from "http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSTableViewDelegate_Protocol/Reference/Reference.html" (sorry, StackOverflow won't let me post more than one hyperlink yet)
Hope this helps.
I have an NSComboBox and I was wondering if there is an easy way to convert it to an ordinary NSTextField? Considering NSCombBox is a subclass of NSTextField, I would think there is an easy way to turn this on/off but I can't figure it out.
They are two separate controls. You'll just have to swap one out for the other. If you're looking to do this in Interface Builder at design time, it's mind-numbingly simple. If you're looking to do this in code, it's not overly difficult (especially if you already have both controls created and configured somewhere in your XIB, like an off-screen view).
It might help to describe what you're trying to accomplish (ie, why you want to swap the controls). It's difficult to give a succinct answer without more information about your goals.