Exporting NSTableView to HTML [Open Source]

I needed to export the contents of a NSTableView to HTML, so I wrote a little NSTableView subclass in Objective-C to do this.

ATHyperTextTableView is a simple NSTableView subclass that makes exporting a table view to HTML easy. You can customize the look of the exported HTML table with your own CSS too.

Screenshots Below:
Screenshot of NSTableView.
Screenshot of exported HTML from a the tableView, loaded into a WebView.

Screenshot of the exported HTML loaded into a WebView.


Screenshot of exported HTML from a the tableView, loaded into a WebView with custom CSS set.

Screenshot of the exported HTML loaded into a WebView, styled with custom CSS.

There is a sample project available on Github here.

How to Disable NSScrollView Scrolling

Unlike UIScrollView on iOS, NSScrollView on Mac does not have a handy scrollEnabled property for you to set to NO if you need to temporarily disable scrolling in your app.

If you Google around, you can find several posts on websites like stackoverflow that ask questions like How can I disable the vertical scrolling of a NSScrollView? Some have suggested setting the hasVerticalScroller property to NO as the answer. However, the hasVerticalScroller property only effects the visibility of the scroller, a value of NO does not actually prevent scrolling.

There are also ways to constrain scrolling from an NSView subclass, as described in Apple’s documentation here. Constraining scrolling from a view subclass can be useful, but what if you want to just temporarily disable scrolling?

The easiest way to disable scrolling it seems is to subclass NSScrollView and add a BOOL property with a name like scrollingEnabled. Then you can simply override the designated initializers (initWithFrame: and initWithCoder:) and set the ivar of the property to YES as the default value:

-(instancetype)initWithFrame:(NSRect)frameRect 
{ 
   self = [super initWithFrame:frameRect]; 
   if (self) 
   { 
       [self setUpOnInit]; 
   } 
   return self; 
}
 
- (instancetype)initWithCoder:(NSCoder *)coder 
{ 
    self = [super initWithCoder:coder]; 
    if (self) 
    { 
         [self setUpOnInit]; 
    } 
    return self; 
}
 
-(void)setUpOnInit
{
   //Set all default values. 
   _scrollingEnabled = YES; 
}

Now you can just override the scrollWheel: method and check the property:

-(void)scrollWheel:(NSEvent *)theEvent 
{ 
    if (self.scrollingEnabled) {  
      [super scrollWheel:theEvent];  
     } 
    else {  
       //scrolling is disabled. 
    } 
}

This technique will work in most cases, but there may be times when a view inside of a scroll view implements autoscrolling behavior (perhaps if the view is a dragging destination). If you need to temporarily disable scrolling in such a case, you may want to subclass NSClipView and block scrolling from the appropriate methods there. It would probably be better if Apple just added a scrollingEnabled property on NSScrollView.

Ten Different Takes on Objective-C Since 2011

I thought it would be fun to post quotes from various articles/blog posts I found around the web about the Objective-C programming language. Objective-C is still my primary programming language. I would like to believe that for Swift to win, Objective-C does not necessarily have to lose. I cannot help but wonder if many are discrediting the language because it is simply a trendy thing to do on the blogosphere. Has the tone changed?

2011

Alex Payne, when asked “Are you surprised at the popularity of any current languages?” during the preview of emerging languages for OSCON 2011’semerging languages track:

Alex Payne: I’m constantly surprised at the popularity and success of Objective-C. Almost everyone I know tells the same story about Objective-C: they started learning it and they hated it. They thought it was the worst of C and the worst of dynamic languages. And then eventually, they learned to love it. Most of the time, that software works pretty darn well, so who am I to judge? I’m pleasantly surprised by the continued success of Objective-C, and I think it should be taken as a lesson for the language designers out there.

*Blog Post Title: The surprising thing about Objective-C…
http://blog.metaobject.com/2011/07/surprising-thing-about-objective-c.html
In the past few years, quietly, almost invisibly, Apple has transformed its Objective-C language into the best language available. I have been working with Objective-C since the release of the iPhone App Store in 2008. In that time Objective-C has evolved from a clunky, boilerplate-heavy language, into a tight, efficient joy. It is an amazing tool. Anything that I would not write in C I would want to write in Objective-C, were support available. The changes that made Objective-C this way are fairly recent. Basically, they were introduced in 2011, and weren’t supported by enough iOS devices until some time after that. Developers have been able to observe the new features only recently. So I am fairly certain that what I have to say is news.

*Blog Post Title: Apple Could Power the Web
http://kevinlawler.com/objective-c
2012

It is interesting to note that both Java and C++ are losing ground while Objective-C is gaining significantly,€ said Don Babcock, a software engineer at Wake Forest University Health Services. €œI’ve always disliked C and C++ because of all the €˜warts€™ [pointers, memory management hassles, etc.] and I’m a longtime fan of Java [ever since the 1.1 days]. However, my new favorite language is Objective-C because it has all of the best advantages of both.€

*Blog Post Title: Objective-C Overtakes C++ in Programming Language Popularity
http://www.eweek.com/c/a/Application-Development/ObjectiveC-Overtakes-C-in-Programming-Language-Popularity-645924
Though the Apple App Store is the main reason for the recent rise of the language, Eric Shapiro, technology chief of app developer ArcTouch, also argues that the language is easier to use than most. “Almost anybody can just pick up a book and learn [Objective C] basics,” he tells Wired. “That doesn’t make you an expert, but that does mean that so many more [developers] are familiar.”

*Article Title: iPhone Coding Language Now World’s Third Most Popular
http://www.wired.com/2012/07/apple-objective-c/
Obj-C is a beautiful and simple extension to plain vanilla C, giving you both the simplicity of C, and the power of C++. It is a major reason why the Apple toolbox is as good as it is, and the apps work as well as they do. Don’t underestimate that.

Java looks more like an extension to xml, with its endless nesting. Where Obj-C teaches you good programming habits, java does the opposite. In even basic java templates, you will see methods with the same name. In my first project, made from Wenderlich’s tut, I never dropped below 200 warning. Two hundred. Inline overrides and methods might be good for quick fixes, but they will quickly turn your code into a bloated mess. There are no headers, to in a glance tell you what a class does, and you will in no time lose track of what you are doing. I could go on.

[…]

I strongly disagree that java is anywhere near as good a programming language as Obj-C. I actually believe, that the reason that I am a good programmer – NOT implying that you are not 😉 – is because I keep it simple. And for that, I need a simple language, and a simple editor. And a biscuit and some water …

*Cocos2d Forum Post in Thread Titled: 30 days of Android
http://forum.cocos2d-objc.org/t/30-days-of-android/7844
2013

I cut my programming teeth on Java, and didn’t think much of Objective-C at first, in large part because of its excessive verbosity: a line like

String s2 = s1.replace(“abc”,”xyz”);

becomes

NSString *s2 = [s1 stringByReplacingOccurrencesOfString:@”abc” withString:@”xyz”];

but I’ve grown very fond of Objective-C. It’s just better and cleaner than Java.

*Article Title: Android vs. iOS Development: Fight!
http://techcrunch.com/2013/11/16/the-state-of-the-art/
2014

When the iPhone came out, the Objective C language became a hot topic. I was so excited about the thought of writing games for iPhone that I forced myself, despite wariness at [[funny foreign] syntax], to learn Objective C and begin writing a game. Boy was I surprised when I came to love Objective C’s simple elegance and powerful Foundation classes.

Now, after nearly three years of making games in Objective C with Cocos2D-iPhone, I’ve discovered Cocos2D-X and unearthed a whole new love for C++.

[…]

If Objective C is an automatic transmission car, then C++ is a manual.

One might argue that the automatic car is superior because it’s easier. However, that one hasn’t had the experience of clutching into neutral across a gravelly patch to avoid spinning the tires.

C++ has more speed and control, yet it requires a greater level of skill from the programmer.

[…]

Readability

Here’s another fundamental difference. Objective C is inherently more readable than C++ because method parameters are built into the name of the method. Consider:

// Objective C
[layer addChild:sprite z:1];
// C++
layer->addChild(sprite, 1);

*From the online book: How to Make a Platformer Game with Cocos2D-X
http://wizardfu.com/book/cocos2d-x/cpp-vs-objective-c/
Even without knowing a thing about programming, it’s easy to understand this is a big deal: Apple’s current language of choice, Objective-C, has been in the mix for Macs since the launch of OS X, and for iPhones and iPads from day one. For Apple to throw out the developer playbook across its entire product portfolio suggests there’s a big change afoot — and many devs aren’t sad to see it happen. “[Objective-C’s] a real pain,” says Alex Chung, the co-founder of Giphy, who has created multiple iOS apps. “If you’re off by one character it all falls apart. It’s just really old school, you know, it’s like speaking Latin.”

*Article Title: The Swift effect: Apple’s new programming language means way more iPhone developers and apps
http://www.theverge.com/apple/2014/6/2/5773928/apple-swift-programming-developers-objective-c
Indeed, Swift is much, much simpler than Objective-C, with syntax that barely appears like English to the average person. Here’s how you would print “Hello, my name is Swift” using the language’s print command:

let name = “Swift”
println(“Hello”)
println(“My name is \(name)”)

You can see how it’s a little more understandable than the garbled Objective-C code below:


NSString *name = @”Swift”;
NSLog(@”Hello my name is %@”, name);

*Article Title: Go Ahead, Tim Cook, Write An App In Swift—We Dare You
http://readwrite.com/2015/02/10/tim-cook-swift-app-objective-c
IS SWIFT EASIER TO LEARN?
From a pure programming standpoint, you’re almost guaranteed to be more productive using Swift. Of course, if you’re an expert Objective-C developer, that might not be the case.

Swift is the obvious choice if you’re new to iOS and looking to get up to speed quickly. The language will stay out of your way allowing you to focus on the things that matter: making your app work the way you want it to!

*Blog Post Title: Swift vs. Objective-C
http://learn.onemonth.com/swift-vs-objective-c

So which is it? Is Objective-C readable or too verbose? Is Objective-C complicated (like Latin?) or is it so easy that anyone can pick up a book and learn the basics? I personally did not find Swift any easier or harder than Objective-C when adding Swift support to Visual Attributed String. I wonder how much, if any, of the praise and criticism expressed in these quotes, has to do with influence from a very powerful, external force.

Visual Attributed String: An Essential Tool for iOS and Mac Developers Released on the Mac App Store!


             

Visual Attributed String has been released on the Mac App Store! Visual Attributed String is the most advanced WYSIWYG (what you see is what you get) editor for attributed strings, providing rich features including:

-Platform Specific Code: generate code to create NSAttributedStrings for both iOS and OS X.
-Generates both Swift AND Objective-C code!
-Get the code for your entire document by clicking “Code” in the toolbar!
-Get the code for only selected portions of text by right-clicking and selecting the “Code for Selection” menu item! This allows you to get code for individual words/paragraphs as you need them on a long document with many different text styles!
-Save your text as an image to use for the web! You can also generate images for only selected portions of text by right-clicking and selecting the “Image for Selection” menu item! Supported image formats include: .png, .bmp, .tiff, and .jpeg!
-Smart Code Generation: Visual Attributed String is smart enough to generate code for non-mutable attributed strings (NSAttributedString) and mutable attributed strings (NSMutableAttributedString) based on your typed text!
-Easily add emoji and special characters to your attributed strings by clicking the “Special Characters” toolbar item to bring up the characters palette!
-Change the background of the entire document to see how your text looks against different background colors. If you want to get the code for your document’s background color, you can simply right click and select the “Background Color Code” menu item!
-Add images to your attributed strings! Just drag and drop an image from Finder onto the window to add images to the document! Did you know NSAttributedString is capable of displaying images?
-Custom Font Warning: Visual Attributed String will warn you if your document uses a custom font that is not included in the iOS SDK!
-Visual Attributed String is a document-based application and supports features such as autosaving, duplicating documents, renaming documents, and reverting document versions!
-Powerful yet simple interface for styling text! Visual Attributed String provides an interface for editing many attributes including:
       ∙Font (NSFontAttributeName)
       ∙Font Color (NSForegroundColorAttributeName)
       ∙Font Background Color (NSBackgroundColorAttributeName)
       ∙Kern (NSKernAttributeName)
       ∙Skew (NSObliquenessAttributeName)
       ∙Shadow (NSShadowAttributeName)
       ∙Underline (NSUnderlineStyleAttributeName)
       ∙Underline Color (NSUnderlineColorAttributeName)
       ∙Strikethrough (NSStrikethroughStyleAttributeName)
       ∙Strikethrough Color (NSStrikethroughColorAttributeName)
       ∙Link (NSLinkAttributeName)
       ∙Stroke (NSStrokeWidthAttributeName)
       ∙Stroke Color (NSStrokeColorAttributeName)
       ∙Paragraph Style (NSParagraphStyleAttributeName)
       ∙Tool Tip (NSToolTipAttributeName)
       ∙Attaching Images (NSAttachmentAttributeName)

For more information on Visual Attributed String you can go to the Mac App Store or the official Visual Attributed String website.

Screenshots:

Visual Attributed String Mac app screenshot of document window showing some editable attributes. Visual Attributed String Mac app screenshot showing mocked up iOS application user interface. Visual Attributed String Mac screenshot showing generated code for a document in Swift. Visual Attributed String Mac app screenshot showing code for selection popover in document. Visual Attributed String Mac App screenshot showing Emoji characters in document.

How to Deprecate your Own API in Objective-C

To mark some of your own API as deprecated in Objective-C, you simply can add the following attribute to your method declaration (as seen in the snippet below):

-(void)myOldMethod __attribute((deprecated("Use the myNewMethod instead.")));

Now when you call myOldMethod in your code, the compiler will warn you that: “myOldMethod is deprecated. Use the myNewMethod instead”. You can also add the deprecated attribute to property declarations.

But what if you wanted to be more specific? For instance, if there is a better way to handle something in the iOS 8 SDK, you can mark an old method as deprecated only for projects that have iOS 8.0 or later as the deployment target.

-(void)myOldMethod NS_DEPRECATED_IOS(3_0, 8_0,"Use myNewMethod instead.");

Using the code snippet above, if you call myOldMethod the compiler will only warn you that the method is deprecated if your project’s deployment target is set to iOS 8 or later.

Deprecating your own methods can come in handy especially if you have written your own framework that is used across multiple projects. You may choose to deprecate some of your own API rather than removing the methods entirely until you get around to adapting your existing projects to use your new API.