iphone - 为什么这个自定义 UIView 代码不显示 UILabel 的?

标签 iphone ios uiview uilabel

我有点想知道为什么,当我使用这个自定义 UIView 显示一行 3 个 UILabel 时,虽然我看到框架的红色轮廓,但我在框架内看不到任何东西。那就是标签没有出现,我设置的 UILabel 的黑色边框也没有出现。

有人能在这里发现问题吗?

#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(dateColumnWidth, 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

最佳答案

您似乎没有将 UILabel 添加为 subview 。

关于iphone - 为什么这个自定义 UIView 代码不显示 UILabel 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5427676/

相关文章:

ios - 如何以编程方式在 iOS 的 iAds 横幅上显示广告

ios - 如何为 iOS 的 Cordova 插件提供 Aviary Apikey 和 Secret

ios - Mach-O 二进制 : external undefined symbols not found after patching symtab

ios - 跳转到 CSV 文件中的行

iphone - 如果我们在真正的预提交处理程序中,由于 CA 的限制,我们实际上不能添加任何新的围栏——这是否意味着什么?

ios - 在 UIView 上绘制文本更好,还是使用 UILabel 并在运行时更新文本?

ios - 当上传百分比增加时,如何使 UIView 的宽度增加? swift 4

iphone - 如何设置 CMake 为 iPhone 构建库

iphone - 我可以在没有ios设备的情况下将应用程序提交到应用程序商店吗?

iphone - PhoneGap 3/Cordova 3 不使用 native 邮件应用程序 (ios) 打开电子邮件链接 (mailto)