ibireme / YYText

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

AutoLayout的问题 #269

Closed lexiaoyao20 closed 8 years ago

lexiaoyao20 commented 8 years ago
//label 初始化代码
_titleLabel = [YYLabel new];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = [UIFont systemFontOfSize:17];
    _titleLabel.textColor = [themeMgr colorForKey:@"TextColor"];
    _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _titleLabel.displaysAsynchronously = YES;
    _titleLabel.ignoreCommonProperties = NO;
    _titleLabel.fadeOnAsynchronouslyDisplay = NO;
    _titleLabel.fadeOnHighlight = NO;
    _titleLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    [self.containerView addSubview:_titleLabel];

//使用Masonry进行自动布局
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.left.right.equalTo(self.containerView);
    }];

我设置了numberOfLines=2,使用了自动布局,但是只显示了一行,不知道是哪里出问题了,麻烦YY大神帮我看下

我使用的YYText的版本是 - YYKit (1.0.3) 示例图

ibireme commented 8 years ago

设置一下 preferredMaxLayoutWidth 看看。

lexiaoyao20 commented 8 years ago

设置了 preferredMaxLayoutWidth 可以解决问题 不过我试了UILabel,不用设置preferredMaxLayoutWidth也能显示两行,这个能不能改进下?

ibireme commented 8 years ago

目前没有什么好的办法。。如果你有好办法希望能帮忙改进一下这块儿。 (另外,UILabel 在某些情况下不设置这个属性也会有问题。。)

LawrenceHan commented 8 years ago

别偷懒。。。直接用YYTextLayout算出高宽,然后赋值就可以了。。。 @lexiaoyao20

wy19901227 commented 8 years ago

这不是偷懒,人家用自动布局呢,高度自然不用算,但是这个布局的宽度你必须要设置,ios7以后uilabel不用设置,估计是apple在布局的时候,自己算的

发自我的 iPhone

在 2016年4月8日,14:40,Lawrence notifications@github.com 写道:

别偷懒。。。直接用YYTextLayout算出高宽,然后赋值就可以了。。。

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

LawrenceHan commented 8 years ago

如果只是普通label,不要用YYLabel。YYLabel是为了复杂需求和性能设计的。

starFelix commented 8 years ago

监听frame变化,同步设置preferredMaxLayoutWidth

kharmabum commented 6 years ago

Can some explain in English how to approach this problem? I'd really appreciate it! cc @ibireme @wy19901227 🙇

kharmabum commented 6 years ago

Well. I think I figured it out. By setting preferredMaxLayoutWidth to the max available width of the label, autolayout appears to be working correctly. @ibireme, thanks for such an incredibly powerful library.