iPhone: drawing text

Discussion in 'Mac Programming' started by rand0m3r, Apr 20, 2008.

  1. rand0m3r macrumors regular

    Joined:
    Jun 30, 2006
    #1
    hi i read the apple docs and followed this line of code:

    [myText drawAtPoint:CGPointMake(10,10) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];

    but my string doesn't get printed. does anyone know how to draw text on a custom view for the iphone?
     
  2. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #2
    The drawing has to be done inside of a view. Did you make a view before drawing the string?

    Like maybe in a file ending in .h do...

    Code:
    @class MyView;
    
    @interface iPhoneTestAppDelegate : NSObject {
        UIWindow *window;
        MyView *contentView;
    }
    
    @property (nonatomic, retain) UIWindow *window;
    @property (nonatomic, retain) MyView *contentView;
    
    @end
    Code:
    #import "iPhoneTestAppDelegate.h"
    #import "MyView.h"
    
    @implementation iPhoneTestAppDelegate
    
    @synthesize window;
    @synthesize contentView;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {	
    	// Create window
    	self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        
        // Set up content view
    	self.contentView = [[[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    	[window addSubview:contentView];
        
    	// Show window
    	[window makeKeyAndVisible];
    }
    
    - (void)dealloc {
    	[contentView release];
    	[window release];
    	[super dealloc];
    }
    
    @end
    Do that first.

    Then in another file say MyView.h add:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface MyView : UIView {
    }
    
    @end
    ...and in MyView.m add...

    Code:
    #import "MyView.h"
    
    @implementation MyView
    
    @end
    And in there add your bits to draw a string?

    If not. Sign up to be an iPhone developer - its free. Download the sample code and the SDK. They are also free. Your time, well that's probably not really free, but if you spend enough you might learn something.
     
  3. Duke Leto macrumors regular

    Joined:
    Mar 17, 2008
    #3
    You could also make a UILabel in that View class a a property. From there you can change the text and do stuff with the UILabel (setting color, size, place, the actual output, etc.).
     

Share This Page