开发者

why does this custom UIView code not display the UILabel's?

I'm kinda stuck trying to see why, when I use this custom UIView to show a row of 3 x UILabels, that whilst I see the outline in RED for the frame, I don't see anything inside the frame. That is the labels don't appear, nor does the black border for the UILabels which I've set.

Can anyone spot an issue here?

#import <UIKit/UIKit.h>
@interface LabelRowView : UIView {
    UILabel *_dateLabel;
    UILabel *_titleLabel;
    UILabel *_locationLabel;
}
@property (nonatomic, retain) UILabel *dateLabel;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *locationLabel;
- (void)setFont:(UIFont *)newFont;
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr;
@end

// ==============================================================

@implementation LabelRowView
    @synthesize dateLabel = _dateLabel;
    @synthesize titleLabel = _titleLabel;
    @synthesize locationLabel = _locationLabel;

- (void)setDefaultsForLabel:(UILabel*)label {
    label.layer.borderColor = [UIColor blackColor].CGColor;
    label.layer.borderWidth = 1.0;
    label.textAlignment = UITextAlignmentLeft;
    label.lineBreakMode = UILineBreakModeTailTruncation;
    label.opaque = YES;   // TODO: put back in for performance reasons if possible
    label.backgroundColor = [UIColor clearColor];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.dateLabel      = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease];       // Position will be updated later
        self.titleLabel     = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease];       // Position will be updated later
        self.locationLabel  = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease];       // Position will be updated later

        // Set Defaults
        [self setDefaultsForLabel:self.dateLabel];
        [self setDefaultsForLabel:self.titleLabel];
        [self setDefaultsForLabel:self.locationLabel];

        self.layer.borderColor = [UIColor redColor].CGColor;
        self.layer.borderWidth = 1.0;
    }
    return self;
}

- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr {
    self.dateLabel.text = dateStr;
    self.titleLabel.text = titleStr;
    self.locationLabel.text = locationStr; 
}

- (void)setFont:(UIFont *)newFont {
    self.dateLabel.font = newFont;
    self.titleLabel.font = newFont;
    self.locationLabel.font = newFont;
}

- (void)layoutSubviews {
    [super layoutSubviews];
        // Get current width for this subview 
        CGFloat contentViewWidth = self.frame.size.width;
        CGSize maximumLabelSize = CGSizeMake(contentViewWidth, 9999);  
        CGFloat oneLineHeight = [@"A" sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].height;

        // Calculate Widths
        CGFloat dateColumnWidth = [@"Ddd 12:22 " sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].width;
        CGFloat titleColumnWidth = (contentViewWidth - dateColumnWidth) * 2.0/3.0;
        CGFloat locationColumnWidth = contentViewWidth - dateColumnWidth - titleColumnWidth;

        // Date - Layout 
        CGRect newRect = CGRectMake(0.0, 0.0, dateColumnWidth, oneLineHeight);
        self.dateLabel.frame = newRect;

        // Date - Layout 
        newRect = CGRectMake(d开发者_开发百科ateColumnWidth, 0.0, titleColumnWidth, oneLineHeight);
        self.titleLabel.frame = newRect;

        // Title - Layout 
        newRect = CGRectMake(dateColumnWidth + titleColumnWidth, 0.0, locationColumnWidth, oneLineHeight);
        self.locationLabel.frame = newRect;
}

- (void)dealloc
{
    [super dealloc];
}

@end


You don't seem to be adding the UILabels as subviews.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜