UILabel自适应行数与高度
效果图:
- (void)viewDidLoad {
//初始化label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, kUI_SCREEN_WIDTH - 20, 100)];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = @"本店于十一期间特推出一系列优惠,限时限量敬请选购!沙发:钻石品质,首领风范!床垫:华贵典雅,彰显时尚!尊贵而不失奢华,典雅却不失自然!温馨和浪漫的生活,我们与你一同创造!";
CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)];
label.frame =CGRectMake(10, 10, kUI_SCREEN_WIDTH - 20, size.height);
label.font = [UIFont systemFontOfSize:14];
[labelView addSubview:label];
label.backgroundColor = [UIColor grayColor];
}
UILabel自适应行数与高度、封装好方法
效果图:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, kUI_SCREEN_WIDTH - 20, 20)];
label.text = @"本店于十一期间特推出一系列优惠,限时限量敬请选购!沙发:钻石品质,首领风范!床垫:华贵典雅,彰显时尚!尊贵而不失奢华,典雅却不失自然!温馨和浪漫的生活,我们与你一同创造!";
label.backgroundColor = [UIColor yellowColor];
[self numberOfAdaptiveRowsUILabel:label systemFontOfSize:15.0f];
[self.view addSubview:label];
- (CGSize )numberOfAdaptiveRowsUILabel:(UILabel *)label systemFontOfSize:(CGFloat )font{
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)];
label.frame =CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, size.height);
label.font = [UIFont systemFontOfSize:font];
return size;
}