根据my first post的一些说明,我正在做的是
1. create a class ( subclass of uiview)
2. fill it with gradient
3. add a uilabel on it
我在做的是:
1. in xib, i drag and drop uiview onto the screen
2. change to 'customViewBackGround' in 'custom class'
和customViewbackGround如下所示:
-(void) layoutSubviews {
CGFloat height = 20.0;
CGFloat x = 5;
CGFloat y = 3;
CGRect rect = CGRectMake(x, y, self.bounds.size.width - 2 * x, height);
titleLabel.frame = rect;
titleLabel = [[UILabel alloc] init] ;
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.textColor = [UIColor blackColor];
[self addSubview:titleLabel];
}
- (void)drawRect:(CGRect)rect {
titleLabel.text = @"How to redeem your e-gift card online";
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0
green:230.0/255.0
blue:230.0/255.0
alpha:1.0].CGColor;
CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;
CGRect paperRect = self.bounds;
// Fill with gradient
drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);
// Add white 1 px stroke
CGRect strokeRect = paperRect;
strokeRect.size.height -= 1;
strokeRect = rectFor1PxStroke(strokeRect);
CGContextSetStrokeColorWithColor(context, whiteColor);
CGContextSetLineWidth(context, 1.0);
CGContextStrokeRect(context, strokeRect);
// Add separator
CGPoint startPoint = CGPointMake(paperRect.origin.x, paperRect.origin.y + paperRect.size.height - 1);
CGPoint endPoint = CGPointMake(paperRect.origin.x + paperRect.size.width - 1, paperRect.origin.y + paperRect.size.height - 1);
draw1PxStroke(context, startPoint, endPoint, separatorColor);
}
然而,uilabel的文字毕竟没有出现。 关于这个问题的任何想法。
答案 0 :(得分:2)
在初始化之前,您无法设置titleLabel的框架。交换这两行:
titleLabel = [[UILabel alloc] init] ;
titleLabel.frame = rect;
答案 1 :(得分:0)
在layoutSubviews
中,您在titleLabel创建之前设置框架。在创建标签后设置它。