自定义 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,您的 LetterLabel 需要在某个时候调用 UILabel 的 drawRect: 吗?
Surely your LetterLabel needs to call the UILabel's drawRect: at some point?