ibireme / YYText

Powerful text framework for iOS to display and edit rich text.
MIT License
8.86k stars 1.7k forks source link

YYText在约束下,一直不好用. 有什么算高的方法没? #407

Closed xclidongbo closed 8 years ago

xclidongbo commented 8 years ago

RT

xclidongbo commented 8 years ago

代码如下:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureView];
    }
    return self;
}

- (void)configureView {
    UIView * contentView = self.contentView;

    self.backView = [[UIView alloc] init];
    self.backView.backgroundColor = [UIColor blueColor];
    [contentView addSubview:_backView];
    self.backView.layer.cornerRadius = 5;

    self.chatLabel = [[YYLabel alloc] init];
    self.chatLabel.backgroundColor = [UIColor yellowColor];
    [self.backView addSubview:self.chatLabel];
    self.chatLabel.numberOfLines = 0;
    self.chatLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.bounds)-20;
//    self.chatLabel.displaysAsynchronously = YES;
    self.chatLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    self.chatLabel.textAlignment = NSTextAlignmentLeft;

    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self).insets(UIEdgeInsetsZero);
        make.width.mas_equalTo(self);
    }];

    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(contentView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];

    [_chatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_backView).insets(UIEdgeInsetsMake(5, 5, 5, 5));

    }];

    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_backView.mas_bottom).offset(5);
    }];

}

- (void)setValueWithModel:(PublicChatModel *)model {
    NSMutableAttributedString * text = [NSMutableAttributedString new];
    UIFont * font = [UIFont systemFontOfSize:16];

    UIImage * image = [UIImage imageNamed:@"06_porfile_like_icon"];
    NSMutableAttributedString * attributeStr = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
    [text appendAttributedString:attributeStr];
    [text appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",model.nickName] attributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]}]];
    [text appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" : %@",model.message] attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]];

    text.yy_alignment = NSTextAlignmentLeft;
    text.yy_font = [UIFont systemFontOfSize:16];
    self.chatLabel.attributedText = text;

}
Sofreda commented 7 years ago

同问,使用SDAutoLayout 布局,计算出来yylabel的高是0