gsdios / SDAutoLayout

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.
MIT License
5.9k stars 1.28k forks source link

cell自动计算高度出了问题 #300

Open halocq opened 5 years ago

halocq commented 5 years ago

image 标签个数,长度不确定的,参照了你写的微信demo评论那块

halocq commented 5 years ago

` self.tags = tags; self.buttonsModel = buttonsModel;

if (self.tagViewsArr.count) {
    [self.tagViewsArr enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj sd_clearAutoLayoutSettings];
        obj.hidden = YES;
    }];
}

CGFloat margin = 15;
UIView *preViewX = self;
UIView *preViewY = self;
UIView *lastTop = self;
CGFloat preWidth = 15;

for (int i = 0; i < self.tags.count; i++) {
    UILabel *label = self.tagViewsArr[i];
    label.hidden = NO;
    NSDictionary *text;
    if ([tags[i] isKindOfClass:[NSString class]]) {
        text = @{@"title": [NSString stringWithFormat:@"#%@", tags[i]]};
    } else {
        text = tags[i];
    }

    if (preWidth + [self getTextWidth:text[@"title"]] + margin > WIN_WIDTH - 30) {
        preViewY = preViewX;
        preViewX = self;
    } else {
        preViewY = preViewY;
    }
    label.sd_layout
    .leftSpaceToView(preViewX, margin)
    .topSpaceToView(preViewY, 5)
    .heightIs(30);

    preViewX = label;
    lastTop = label;
    preWidth += [self getTextWidth:text[@"title"]];
}

if (tags.count) {
    _line.sd_resetLayout
    .leftSpaceToView(self, 10)
    .topSpaceToView(lastTop, 12)
    .widthIs(WIN_WIDTH - 20)
    .heightIs([Helper lineHeight:1]);
} else {
    _line.sd_resetLayout.heightIs(0);
}

_buttonsView.sd_layout
.topSpaceToView(_line, 0)
.leftSpaceToView(self, 0)
.widthIs(WIN_WIDTH)
.heightIs(65);

[self setupAutoHeightWithBottomView:_buttonsView bottomMargin:0];
`