gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:45:05 24 4
gpt4 key购买 nike

我有点想知道为什么,当我使用这个自定义 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com