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