Home > Articles > Operating Systems, Server > MAC OS X/Other

  • Print
  • + Share This
This chapter is from the book

Converting Interface Builder Files to Their Objective-C Equivalents

A handy open-source utility by Adrian Kosmaczewski allows you to convert Interface Builder files to Objective-C code. With it, you can extract all the layout information and properties of your visual design and see how that would be coded by hand. nib2objc does exactly what its name suggests. With it, you can generate converted code that takes into account the class constructors, method calls, and more. It works on .xib and .storyboard files, although some newer features such as segues are not yet exposed; under the hood both formats are simply XML.

Listing 3-2 shows the result of running nib2objc on the .xib file used in the first walkthrough. Compare it to the far simpler (and less thorough) by-hand version in Listing 3-1. It performs more or less the same tasks. It creates a new label and then adds the label to the window. However, this conversion utility exposes all the underlying properties, of which just a few were edited in Listing 3-1.

To peek at the original IB XML, open the storyboard file in Text Edit. Issue open -e from the Terminal command line while in the HelloWorld project folder in the en.lproj subfolder:

open -e MainStoryboard_iPad.storyboard

Listing 3-2. HelloWorldViewController.xib after Conversion to Objective-C

UIView *view3 = [[UIView alloc] initWithFrame:

    CGRectMake(0.0, 20.0, 320.0, 460.0)];

view3.frame = CGRectMake(0.0, 20.0, 320.0, 460.0);

view3.alpha = 1.000;

view3.autoresizingMask =

    UIViewAutoresizingFlexibleRightMargin |


view3.backgroundColor =

    [UIColor colorWithRed:0.963 green:1.000 blue:0.536 alpha:1.000];

view3.clearsContextBeforeDrawing = YES;

view3.clipsToBounds = NO;

view3.contentMode = UIViewContentModeScaleToFill;

view3.hidden = NO;

view3.multipleTouchEnabled = NO;

view3.opaque = YES;

view3.tag = 0;

view3.userInteractionEnabled = YES;

UILabel *view6 = [[UILabel alloc] initWithFrame:

    CGRectMake(72.0, 150.0, 175.0, 160.0)];

view6.frame = CGRectMake(72.0, 150.0, 175.0, 160.0);

view6.adjustsFontSizeToFitWidth = YES;

view6.alpha = 1.000;

view6.autoresizingMask =

    UIViewAutoresizingFlexibleLeftMargin |

    UIViewAutoresizingFlexibleRightMargin |

    UIViewAutoresizingFlexibleTopMargin |


view6.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

view6.clearsContextBeforeDrawing = YES;

view6.clipsToBounds = YES;

view6.contentMode = UIViewContentModeLeft;

view6.enabled = YES;

view6.hidden = NO;

view6.lineBreakMode = UILineBreakModeTailTruncation;

view6.minimumFontSize = 10.000;

view6.multipleTouchEnabled = NO;

view6.numberOfLines = 1;

view6.opaque = NO;

view6.shadowOffset = CGSizeMake(0.0, -1.0);

view6.tag = 0;

view6.text = @"Hello World";

view6.textAlignment = UITextAlignmentLeft;

view6.textColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];

view6.userInteractionEnabled = NO;

[view3 addSubview:view6];

[view2 addSubview:view3];
  • + Share This
  • 🔖 Save To Your Account