我已经制作了一个自定义的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,1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMovetoPoint(UIGraphicsGetCurrentContext(),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,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
解决方法
当然你的LetterLabel需要调用UILabel的drawRect:在某些时候?
-(void)drawRect:(CGRect)rect {
// Your stuff goes here
[super drawRect: rect];
}