2010-03-07 71 views
6

我做了一個自定義的UILabel類,其中我在框架的底部繪製一條紅線。 紅線顯示,但我不能讓文字顯示。自定義UILabel不顯示文本

#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 

回答

20

當然你的LetterLabel需要調用的UILabel的drawRect:在某些時候?

-(void)drawRect:(CGRect)rect { 
    // Your stuff goes here 
    [super drawRect: rect]; 
} 
+0

罪魁禍首是 - [super drawRect:rect]; – 2017-02-10 11:57:04