Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-17-2010, 08:26 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Default UILabel defaults/subclassing question

I have a series of Labels that use the same same font, color, etc. and rather than defining a constant and still having to set the properties for each one I would like to have a couple custom labels to choose from. I thought I could do this by subclassing UILabel but I am not having any luck.

Am I way off track here?

Labels.h
Code:
#import <UIKit/UIKit.h>


@interface Labels : UILabel {

}

@end
Labels.m
Code:
#import "Labels.h"


@implementation Labels


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
     	self.font = [UIFont fontWithName:@"Arial" size:12.0];
		self.textAlignment = UITextAlignmentCenter;
		self.backgroundColor = [UIColor clearColor];
		self.userInteractionEnabled = YES;
		self.textColor = [UIColor whiteColor];
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
    [super dealloc];
}


@end
In my view controller I try to setup the label like so:
Code:
Labels *myLabel = [[Labels alloc] initWithFrame:CGRectMake(15, 190, 130, 20)];
myLabel.text = @"Some text";
myView addSubview:myLabel];
I don't get any compile errors but the label does not appear in the view. Thoughts? Any advice is appreciated.

- Mike
develsolutions is offline   Reply With Quote
Old 03-17-2010, 08:30 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by develsolutions View Post
I have a series of Labels that use the same same font, color, etc. and rather than defining a constant and still having to set the properties for each one I would like to have a couple custom labels to choose from. I thought I could do this by subclassing UILabel but I am not having any luck.

Am I way off track here?

Labels.h
Code:
#import <UIKit/UIKit.h>


@interface Labels : UILabel {

}

@end
Labels.m
Code:
#import "Labels.h"


@implementation Labels


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
     	self.font = [UIFont fontWithName:@"Arial" size:12.0];
		self.textAlignment = UITextAlignmentCenter;
		self.backgroundColor = [UIColor clearColor];
		self.userInteractionEnabled = YES;
		self.textColor = [UIColor whiteColor];
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
    [super dealloc];
}


@end
In my view controller I try to setup the label like so:
Code:
Labels *myLabel = [[Labels alloc] initWithFrame:CGRectMake(15, 190, 130, 20)];
myLabel.text = @"Some text";
myView addSubview:myLabel];
I don't get any compile errors but the label does not appear in the view. Thoughts? Any advice is appreciated.

- Mike
Don't implement a -drawRect method at all, or if you do, just have it call [super drawRect: rect]

Your custom cell isn't doing any custom drawing code, just custom setup.

By having an empty -drawRect method like you do, you're telling the system that your subclass of UILabel will draw itself, and then not doing anything when the system asks one of these objects to draw itself.


Regards,

Duncan C
WareTo
Duncan C is offline   Reply With Quote
Old 03-17-2010, 09:19 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Default

Thanks Duncan - that worked great. Much appreciated.

- Mike
develsolutions is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 233
16 members and 217 guests
ADY, Alsahir, cacao, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:52 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0