views:

404

answers:

2

Hi!

I've made an custom UILabel class in which i draw a red line at the bottom of the frame. The red line is showing but i cant get the text to show.

#import <UIKit/UIKit.h>


@interface LetterLabel : UILabel {

}

@end    


#import "LetterLabel.h"


@implementation LetterLabel


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0.0, self.frame.size.height);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.frame.size.width, self.frame.size.height);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
}


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


@end

#import <UIKit/UIKit.h>
#import "Word.h"

@interface WordView : UIView {
    Word *gameWord;
}

@property (nonatomic, retain) Word *gameWord;

@end

@implementation WordView

@synthesize gameWord;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];

        LetterLabel *label = [[LetterLabel alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
        label.backgroundColor = [UIColor cyanColor];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [label setText:@"t"];
        [self addSubview:label]; 
    }
    return self;
}


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


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


@end
+3  A: 

Surely your LetterLabel needs to call the UILabel's drawRect: at some point?

-(void)drawRect:(CGRect)rect {
   // Your stuff goes here
   [super drawRect: rect];
}
Frank Shearar
A: 

Frank, excellent. I have been ripping my hair due to this. I have subclassed the UILabel and everything worked such as resizing, background color etc. But the text was a no-show until I realized that I had to do [super drawRect: rect]; in the class's overridden drawRect. Thank you!

Kelly