简体   繁体   English

无法显示uilabel的文字,自定义视图

[英]can not show the text of uilabel, custom view

Following some instructions at my first post , what I am doing is 我的第一篇文章中遵循一些指示,我正在做的是

1. create a class ( subclass of uiview)
2. fill it with gradient
3. add a uilabel on it 

What I am doing is : 我在做的是:

1. in xib, i drag and drop uiview onto the screen

2. change to 'customViewBackGround' in 'custom class'

and customViewbackGround looks like below : 和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);
}

However, the text of uilabel is not showing up after all. 然而,uilabel的文本毕竟没有出现。 Any ideas about this issue. 关于这个问题的任何想法。

You can't set the frame of titleLabel before it's been initialized. 在初始化之前,您无法设置titleLabel的框架。 Swap those two lines: 交换这两行:

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

In layoutSubviews you are setting the frame on titleLabel before it is created. layoutSubviews您在titleLabel创建之前设置框架。 Set it after creating the label. 在创建标签后设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM