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 のフレームを設定することはできません。これらの 2 行を入れ替えます。

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