ios不一样版本下的UILabel自动换行问题

@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上面则不会自动换行,直接打省略号。php

正常状况下,numberOfLines设置为0,UILabel就会自动换行了。html

可是在iOS6下面须要设置preferredMaxLayoutWidth,autolayout才会判断到折行的位置,才能正确的显示多行的UILabelios

可是preferredMaxLayoutWidth设置为多少才是正确的呢?app

若是你知道一个确切的width固然是最好的,那么直接设置便可,ui

可是若是UILabel的宽度是自适应的,不肯定,那么能够使用以上的代码设置atom

相关文章
相关标签/搜索