March 12, 2008 4

iPhone Hello World Using UILabel

By Levi Senft in iPhone

In this version of my iPhone Hello World app I’m using a UILabel widget to display the text instead of a UITextView. I’m also centering the label.

helloworldAppDelegate.h:

    1 //
    2 //  helloworldAppDelegate.h
    3 //  helloworld
    4 //
    5 //  Created by Levi Senft on 3/12/08.
    6 //
    7
    8 #import <UIKit/UIKit.h>
    9
   10 @class MyView;
   11
   12 @interface helloworldAppDelegate : NSObject {
   13     UIWindow *window;
   14     MyView *contentView;
   15     // Levi: Define label object
   16     UILabel  *label;
   17 }
   18
   19 @property (nonatomic, retain) UIWindow *window;
   20 @property (nonatomic, retain) MyView *contentView;
   21 // Levi: Declare label as a property
   22 @property (nonatomic, retain) UILabel *label;
   23
   24 @end
   25

helloworldAppDelegate.m

    1 //
    2 //  helloworldAppDelegate.m
    3 //  helloworld
    4 //
    5 //  Created by Levi Senft on 3/12/08.
    6 //  Copyright __MyCompanyName__ 2008. All rights reserved.
    7 //
    8
    9 #import "helloworldAppDelegate.h"
   10 #import "MyView.h"
   11
   12 @implementation helloworldAppDelegate
   13 
   14 @synthesize window;
   15 @synthesize contentView;
   16 // Levi: Tell the compiler to synthesize relevant accessors
   17 @synthesize label;
   18
   19 - (void)applicationDidFinishLaunching:(UIApplication *)application {
   20     // Create window
   21     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
   22
   23     // Set up content view
   24     self.contentView = [[[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
   25     [window addSubview:contentView];
   26
   27     // Levi: Create the dimensions and centered position for our label
   28     // screen width / 2 - label width / 2
   29     CGFloat x = 320/2 - 120/2;
   30     // screen height / 2 - label height / 2
   31     CGFloat y = 480/2 - 45/2;
   32     CGRect rect = CGRectMake(x , y, 120.0f, 45.0f);
   33
   34
   35
   36
   37
   38     // Levi: Create the label.
   39     self.label = [[[UILabel alloc] initWithFrame:rect] autorelease];
   40     // Set the value of our string
   41     [label setText:@"Hello World!"];
   42     // Center Align the label's text
   43     [label setTextAlignment:UITextAlignmentCenter];
   44
   45     // Levi: Add the label to the content view.
   46     [contentView addSubview:label];
   47
   48     // Show window
   49     [window makeKeyAndVisible];
   50 }
   51
   52 - (void)dealloc {
   53     // Levi: Release the label
   54     [label release];
   55     [contentView release];
   56     [window release];
   57     [super dealloc];
   58 }
   59
   60 @end
   61

Tags:

4 Responses to “iPhone Hello World Using UILabel”

  1. sambasivarao says:

    Thank you,

    Your tutorial helped me, to write a simple application.

    i want a bigger text to display on iphone screen. Means number of words are more. when i write a label like that it is not showing full sentence.

    Can you tell me how i will get it done.

  2. Anonymous says:

    gtà(oiiu-èipçiop__oàit(‘u-(ç _utit (_ ç-è_eè(àçi(‘à’à)(àç oitujjuo_iopùkuujjjjjjjjjjyiiiiiiiiiiiiiiiiioujuyoiùopujuopi^àupuko^p _uoèiuoppyiujyioyuoiroioàtotpotototooyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyihlkimoioooooooo ppp$=^^^^^^^^^^^^^^^^^^^^^^^^^^^plopo^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  3. Ashok says:

    the content what we are added is not stand still in the screen. It appears & disappears when we press the down button. What we have to do inorder to make the text to stand still in the screen?

Leave a Reply