Easily Bind an NSProgress Object to an NSProgressIndicator in Objective-C [Open Source]

In UIKit UIProgressView has an observedProgress property. If you set the observedProgress property on a UIProgressView, it will automatically update its appearance when you make changes to the NSProgress object. On macOS (in AppKit at least) NSProgressIndicator does not have an equivalent API. I created a simple category on NSProgressIndicator that adds an observedProgress property on NSProgressIndicator. The source code is available on Github here.

Adding Force Touch Features to macOS Apps with a Custom Gesture Recognizer [Open Source]

I created a simple subclass of NSGestureRecognizer, ATForceTouchGesture, to simplify the process of adding Force Touch features to macOS apps. You can use this gesture recognizer to add a feature like force clicking to start editing a label (see the screenshot below).

Image captures 'force click' to edit using ATForceTouchGesture.

There is a sample project available on Github here.

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 should also subclass NSClipView and block scrolling by overriding the -constrainBoundsRect: method like this:

-(NSRect)constrainBoundsRect:(NSRect)proposedBounds
{
    MyScrollViewSubclass *mainScrollView = (MyScrollViewSubclass*)self.superview;
 
    if (mainScrollView.scrollingEnabled)
    {
        return [super constrainBoundsRect:proposedBounds];
    }
    else
    {
        //Disabled
        return self.documentVisibleRect;
    }
}

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 modifying your existing projects to use your new API.