NSTableView Zoom In Zoom Out

NSTableView Zoom In Zoom Out

Created By: Debasis Das

In this article we will create a small Cocoa App having One NSTableView and a horizontal Slider (similar to iTunes Volume Control) to magnify the content of the table view.

While magnifying the content of the table view the following will be changed/ magnified

  • Table View Row Height
  • Font Size of the Data Cell
  • Table Column Width

The figure given below is an illustration of the output of the sample code.

As a different approach, developers can also provide a NSPopup or NSCombobox to the users to select the Zoom Percentage and accordingly store/use RowHeight and ColumnWidth Corresponding to the Popup Item Selection and accordingly change the row height and col width of the NSTableView

NSTableViewZoom_Slider

NSTableView with Zoom Slider

NSTableViewZoom_Slider1

//
//  AppDelegate.h
//  TableViewZoom
//
//  Created by Debasis Das on 10/27/14.
//  Copyright (c) 2014 Knowstack. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSTableViewDelegate, NSTableViewDataSource,NSApplicationDelegate>
@property (nonatomic, assign) IBOutlet NSTableView *mainTableView;
@property (nonatomic, assign) IBOutlet NSSlider *zoomInZoomOutSlider; //The min and max zoom value is controller through the slider
@end

//
//  AppDelegate.m
//  TableViewZoom
//
//  Created by Debasis Das on 10/27/14.
//  Copyright (c) 2014 Knowstack. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

#define TABLEVIEW_DEFAULT_ROW_HEIGHT    18 //The default row height of the NSTableView
#define TABLEVIEW_DEFAULT_COL_WIDTH     75 //The default column width of the NSTableView --> Table Columns


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

-(IBAction)zoomInAndZoomOut:(id)sender
{
    float rowHeight;
    float colWidth;
    float vSpacing;
    float zoomFactor = self.zoomInZoomOutSlider.floatValue / 100;
    rowHeight = TABLEVIEW_DEFAULT_ROW_HEIGHT * zoomFactor;
    colWidth = TABLEVIEW_DEFAULT_COL_WIDTH  * zoomFactor;
    vSpacing = 0.2 * rowHeight;
    
    [self.mainTableView setRowHeight:rowHeight];
    for (NSTableColumn *tColumn in [self.mainTableView tableColumns])
    {
        [[tColumn dataCell] setFont:[NSFont systemFontOfSize:(rowHeight - vSpacing)]];
        [tColumn setWidth:colWidth];
    }
    
}

//Demonstration Data Creation
-(NSArray *)dataArray
{
    NSArray *arr = [NSArray arrayWithObjects:
                    [NSDictionary dictionaryWithObjectsAndKeys:@"1000",@"empId",@"Debasis",@"firstName",@"Das",@"lastName",@"debasis_das@knowstack.com",@"email", nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"1001",@"empId",@"Nishant",@"firstName",@"Singh",@"lastName",@"nishant_singh@knowstack.com",@"email", nil],
                    [NSDictionary dictionaryWithObjectsAndKeys:@"1002",@"empId",@"John",@"firstName",@"Doe",@"lastName",@"john_doe@knowstack.com",@"email", nil],
                    nil];
    return arr;
    
}


// TableView Datasource method implementation
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    NSString *aString;
    aString = [[self.dataArray objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]];
    return aString;
}

// TableView Datasource method implementation
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    //we have only one table in the screen and thus we are not checking the row count based on the target table view
    long recordCount = [self.dataArray count];
    return recordCount;
}

@end

Posted in Cocoa, Objective C Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news