即使在layoutSubviews中,UILabel setFrame也没有调整标签大小

时间:2014-10-09 06:56:36

标签: objective-c uilabel frame

我有:

#import "CustomTableViewCell.h"

@interface CustomTableViewCell ()
@property (assign) CGRect titleFrame;
@property (assign) CGRect detailFrame;
@end

@implementation CustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    if (self.title != nil) {
        self.titleFrame = self.title.frame;
    }
    if (self.detail != nil) {
        self.detailFrame = self.detail.frame;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void) setTitleTo: (NSString *)text {
    if (self.title == nil || text == nil) {
        return;
    }

    CGSize maximumLabelSize = CGSizeMake(self.title.frame.size.width, FLT_MAX);
    CGRect expectedLabelSize = [text boundingRectWithSize:maximumLabelSize
                                                  options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:self.title.font}
                                                  context:nil];
    CGRect newFrame = self.title.frame;
    newFrame.size.height = expectedLabelSize.size.height;
    self.titleFrame = newFrame;

    self.detail.lineBreakMode = NSLineBreakByWordWrapping;
    self.detail.numberOfLines = 3;

    [self.title setText:text];
    [self setNeedsLayout];
}

- (void) setDetailTo: (NSString *)text {
    if (self.detail == nil || text == nil) {
        return;
    }

    CGSize maximumLabelSize = CGSizeMake(self.detail.frame.size.width, FLT_MAX);
    CGRect expectedLabelSize = [text boundingRectWithSize:maximumLabelSize
                                                  options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:self.title.font}
                                                  context:nil];
    CGRect newFrame = self.detail.frame;
    newFrame.size.height = expectedLabelSize.size.height;
    self.detailFrame = newFrame;

    self.detail.lineBreakMode = NSLineBreakByWordWrapping;
    self.detail.numberOfLines = 3;

    [self.detail setText:text];
    [self setNeedsLayout];
}


- (void) layoutSubviews {
    [super layoutSubviews];

    if (self.title != nil) {
        NSLog(@"T: %@", self.title.text);
        [self.title setFrame:self.titleFrame];
    }
    if (self.detail != nil) {
        NSLog(@"D: %@", self.detail.text);
        [self.detail setFrame:self.detailFrame];
    }
}
@end

从我正在阅读的内容here,它应调整大小以适应文本。

screen shot

和NSLog输出:

2014-10-08 20:48:29.474 Test[57397:613] T: Largest Metro
2014-10-08 20:48:29.475 Test[57397:613] D: Oahu metropolitan area

我的理解是“欧胡都市区”应该在第二行有“区域”可见。

1 个答案:

答案 0 :(得分:1)

你应该改变三件事:

  • 将标签的尺寸计算移至layoutSubviews
  • 计算标签尺寸的最精确方法是使用-[UILabel sizeThatFits:]来获得所需尺寸。
  • 关闭自动布局