Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Getting Started

Jonathan Ellis edited this page Nov 25, 2013 · 7 revisions

If you don't actually know anything about XML structure, you should probably read a good tutorial before you proceed.

Knowing the rules

The rules of writing UIs in Pegasus are actually pretty simple:-

  1. Tag/element names correspond to UIKit classes. You should drop the "UI" prefix, and tags are case-insensitive. (For example, UIView becomes view, UITextField becomes textfield).

  2. Attribute names correspond to UIKit properties. (For example, the UIKit property textAlignment on class UILabel becomes XML attribute textAlignment on tag label).

  3. Attribute values correspond to structs, enums, primitives and objects. This is where things get a little complicated, because your attribute values are always specified in XML as strings, and then Pegasus translates those strings back into their corresponding struct/enum/primitive/object as required by UIKit. (For example, if you specify the attribute textColor="green" on a label tag, then "green" is translated into the object [UIColor greenColor]). You should have a look at the Types page for more information on how to specify attribute values.

  4. Virtual properties do not correspond directly to UIKit properties, but are a nice feature added to Pegasus to allow simulation of properties that don't actually exist and would normally require more complicated code. For example, UIButton doesn't have a property title; instead, you normally need to do [button setTitle:@"title_goes_here" forControlState:UIControlStateNormal] in UIKit. Because 99% of the time, you are probably going to be using UIControlStateNormal, Pegasus provides a virtual property called title that can be set via an attribute (as above) that shortcuts this for you. The attributes id and tag are also considered virtual properties, and we'll revisit these later.

  5. Nesting elements indicates subviews. If element B is nested within element A, then A becomes B's superviews (and conversely, B is the subview of A). For example, if you create a root element view and then put a label element nested inside of it, then the label becomes a subview of view. (The notable exceptions here are tableview and pickerview, but don't worry about that right now).

A ridiculously simple example

Let's start with a very, very simple example to create a blue view to cover the entire screen of an iPhone:

<view frame="{{0,0}, {320,460}}" backgroundColor="blue" />

Here, you have created a view element (corresponding to UIView) with two attributes: frame and backgroundColor. The values for frame correspond to the values for CGRect struct that specify the view's frame. You can see this listed on the Types page. (In fact, you may have noticed that the format for specifying the view frame is identical to the format accepted by the CGRectFromString() function. That's not particularly surprising, because under the hood Pegasus is using that exact function to create the CGRect from the NSString!)

Notice that frame and backgroundColor are both properties on UIView as documented in the iOS Developer Library Documentation. The same applies for all the other UIView properties.

(A quick aside: Note that frame isn't the only way of positioning your views on the screen, there is also the powerful Layouts feature that can lay out your views automatically based on a layout specification. For now, we'll keep things simple and continue to use frame to position our views.)

The tag is then closed straight away, indicating that this particular view has no subviews.

Building on the example

The previous example wasn't particularly exciting. Let's make things just a little more interesting by adding an image and a label about dinosaurs:

<view frame="{{0,0}, {320,460}}" backgroundColor="blue">
	<imageview id="picture" frame="{{100,40}, {200,150}}" image="dinosaur.png" />
	<label id="prompt" frame="{{40,300}, {240, 30}}" text="Look at the dinosaur!" adjustsFontSizeToFitWidth="yes" backgroundColor="clear" />
</view>

We still have the same view, but we've now added two subviews, an imageview (UIImageView) and a label (UILabel) nested inside the view (UIView).

Notice how each of the subviews have their own frame, and we've also set some other properties. At this stage, you really should begin referring to the Types page to see how strings are are being translated to their corresponding Objective-C types.

We have also introduced the id virtual property, which brings us nicely onto the next step: working with the runtime package; we'll explain id shortly.

Getting started with the runtime package

Until now, we've only looked at building the XML view, but not how you can go about bringing the UI "alive" at runtime.

Make sure you have installed Pegasus according to the instructions. You should then add your XML file containing the UI to your project in Xcode.

In your viewDidLoad method in your view controller, you then need just 3 lines of code to create your view:

    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"my_ui_file.xml"];
    PGView *pegasusView = [PGView viewWithContentsOfFile:filePath];

    [self.view addSubview:pegasusView.view];

Where my_ui_file.xml is your Pegasus XML file. The first line is classic Objective-C, which creates a path to the UI file. The next line instructs PGView (which is the Pegasus adapter for UIView, more on that later) to construct a view with the contents of the XML file.

The returned PGView object then has a property called view which is the underlying UIView which you can then add to your subview just as you would normally.

A full listing of all the views available for use in Pegasus can be found on the Views page.

View adapters?

Pegasus uses the Adapter design pattern to interface UIKit classes with Pegasus.

If that makes no sense to you whatsoever, don't worry. Just think of Pegasus as the "bridge" between XML and UIKit. Each UIKit view class therefore needs an adapter to act as a bridge. For example, the XML element label uses the PGLabel bridge to provide a UILabel object. Each view supported by Pegasus has its own adapter which begins with the prefix PG.

These adapters are actually pretty useful because they provide the id and group properties that can be used to uniquely identify and group views at runtime. They also provide the findViewById: and findViewsInGroup: methods that can be used to search the view hierarchy.

If you're beginning to get confused, this diagram of the overall Pegasus process might help you understand what we've discussed so far:

pegasus overall process

Searching the view hierarchy

So now, let's return to our original dinosaur example. Let's say that at runtime, we want to rewrite the label so that it says "Look at the BIG dinosaur!" instead of simply "Look at the dinosaur!".

You could do that as follows:

    UILabel *promptLabel = (UILabel *)[pegasusView findViewById:@"prompt"];
    promptLabel.text = @"Look at the BIG dinosaur!";

This code probably looks quite familiar to you if you've ever done any Android development.

The UILabel returned by findViewById: is a totally normal UILabel object. You can adjust any of its properties as you would do normally, and can also call methods on it, set delegates, perform animations, etc.

There is a more thorough example of using the runtime package to modify a view provided as part of the Pegasus sample code.

Wrapping it all up

By now you should understand:

  • How to write Pegasus XML using elements and attributes (and their corresponding values).
  • How to load your XML file using the runtime package.
  • How to search the view hierarchy at runtime to reference objects.

Next Steps

  • Read Views to see what view classes are supported in Pegasus.
  • Read Types to understand how to specify attribute values in a way that Pegasus can understand.
  • Read Layouts to learn how to position views without having to painstakingly define a frame for each view.

Clone this wiki locally