2013-04-02 2 views
0

Ниже некоторые инструкции на my first post, что я делаю этоне может показать текст UILabel, настраиваемое представление

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 не появляется в конце концов. Любые идеи по этой проблеме.

ответ

2

Вы не можете установить рамку titleLabel перед ее инициализацией. Поменяйте эти две строки:

titleLabel      = [[UILabel alloc] init] ; 
titleLabel.frame    = rect; 
+0

спасибо, что указал. Это моя глупая ошибка. – tranvutuan

0

В layoutSubviews вы устанавливаете рамки на titleLabel, прежде чем он будет создан. Установите его после создания метки.

Смежные вопросы