0

按照我第一篇文章中的一些说明,我正在做的是

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 的文本毕竟没有显示出来。关于这个问题的任何想法。

4

2 回答 2

2

您不能在 titleLabel 初始化之前设置它的框架。交换这两行:

titleLabel                      =   [[UILabel alloc] init] ;
titleLabel.frame                =   rect;
于 2013-04-02T21:12:00.473 回答
0

layoutSubviews您创建之前在 titleLabel 上设置框架。创建标签后设置它。

于 2013-04-02T21:15:49.910 回答