简体   繁体   English

initWithCoder未设置UILabel属性(UIFont和文本颜色)

[英]initWithCoder doesn't set UILabel property (UIFont and text color)

I have the following code for my UICollectionViewCell 我的UICollectionViewCell有以下代码

@property (strong, nonatomic) IBOutlet UIImageView *storyImage;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;

@end

@implementation Story
@synthesize storyImage;
@synthesize titleLabel;
@synthesize descriptionLabel;

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self)
        {
            [self.contentView addSubview:self.titleLabel];
            [self.contentView addSubview:self.descriptionLabel];
            [self.contentView addSubview:self.storyImage];

            [self.titleLabel setFont:[UIFont fontWithName:kProximaNovaBold size:15]];
            [self.descriptionLabel setFont:[UIFont fontWithName:kProximaNova size:13]];
            [self.descriptionLabel setTextColor:[UIColor colorWithWhite:140/255.f alpha:1.0]];
            self.descriptionLabel.frame  = CGRectIntegral(self.descriptionLabel.frame);
            self.descriptionLabel.center = roundedCenterPoint(self.descriptionLabel.center);

        }
        return self;
    }

but for some reason it's not setting up the property. 但由于某种原因,它没有设置媒体资源。 I'd have to move it out of the init method and put it inside a separate method and call this after calling: 我必须将其移出init方法,并将其放在单独的方法中,并在调用后调用此方法:

 dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath

and then it would work. 然后它会工作。 Any idea why? 知道为什么吗?

You want to be doing this stuff in awakeFromNib: when everything from the nib has actually been loaded and connected 您想在awakeFromNib:做这些事情awakeFromNib:当笔尖中的所有内容均已实际加载并连接时

From the docs for NSObject UIKit Additions 从文档获取NSObject UIKit添加

Discussion 讨论
The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. 笔尖加载基础结构向从笔尖存档重新创建的每个对象发送awakeFromNib消息,但是仅在存档中的所有对象均已加载并初始化之后。

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

相关问题 如何将CGFloat设置为UILabel的文本属性 - How to set a CGFloat to a UILabel's text property UILabel文本没有自动换行 - UILabel text doesn't word wrap UILabel lineBreakMode剪辑不剪辑文本 - UILabel lineBreakMode clip doesn't clip text 使用NSAttributedString为UILabel设置突出显示的文本颜色。 - Set highlighted text color for UILabel using NSAttributedString.? 如果我在nib中设置文本颜色并在资源中定义该颜色,则UILabel文本颜色不会以编程方式更改 - UILabel text color is not changing programmatically, if I set the text color in nib and define that color in assets UILabel文本阴影不能是任何颜色而是灰色 - Can't get UILabel text shadow to be any color but gray stretchableImageWithLeftCapWidth:topCapHeight在initWithCoder中不起作用:UIImageView子类 - stretchableImageWithLeftCapWidth:topCapHeight doesn't work in initWithCoder: of UIImageView subclass 收到通知后,UILabel文本不会更新 - UILabel text doesn't update after receiving notification 分组表视图标题中的UILabel不换行 - UILabel in a grouped table view's header doesn't wrap text 如果UILabel的内容不适合,请更改文本末尾的默认“...” - Change the default '…' at the end of a text if the content of a UILabel doesn't fit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM