@interface ThreadPreviewCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation ThreadPreviewCell
- (void)layoutSubviews{
[super layoutSubviews];
//ios6中需要加上這句話
self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width;
}
- (void)initSubviews
{
self.titleLabel = [[UILabel alloc]init];
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.titleLabel.font = [UIFont systemFontOfSize:14];
self.titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.titleLabel];
}
@end
表現為在iOS7以上的系統中,UILabel能夠自動換行,多行顯示的字符串,而在iOS6上面則不會自動換行,直接打省略號。
正常情況下,numberOfLines設置為0,UILabel就會自動換行了。
但是在iOS6下面需要設置preferredMaxLayoutWidth,autolayout才會判斷到折行的位置,才能正確的顯示多行的UILabel
但是preferredMaxLayoutWidth設置為多少才是正確的呢?
如果你知道一個確切的width當然是最好的,那么直接設置即可,
但是如果UILabel的寬度是自適應的,不確定,那么可以使用以上的代碼設置
