ios动态高度label怎么处理
使用autolayout自适应。
自己计算得到label的高度。
{
CGSize size=[self setTextHeight:_myLabel textW:100];
CGRect rect=_myLabel.frame;
rect.size=size;
_myLabel.frame=rect;
NSLog(@"%@",NSStringFromCGRect(_myLabel.frame));
}
//通过文本的宽度label的文本呢容以及字体大小设置文本的高度
- (CGSize)setTextHeight:(UILabel *)label textW:(CGFloat)textW
{
CGSize size=CGSizeMake(textW, 0);
NSDictionary *attribute = @{NSFontAttributeName: label.font};
CGSize retSize = [label.text boundingRectWithSize:size
options:\
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return retSize;
}
[lblBrief setNumberOfLines:2];
lblBrief.text = [StringUtil subString:strBrief AndSubLength:90];
[btnBrief setImage:[UIImage imageNamed:@"detailBriefDown"] forState:UIControlStateNormal];
[btnBrief setImage:[UIImage imageNamed:@"detailBriefDown"] forState:UIControlStateHighlighted];
}else{
[lblBrief setNumberOfLines:0];
lblBrief.text = strBrief;
[btnBrief setImage:[UIImage imageNamed:@"detailBriefUp"] forState:UIControlStateNormal];
[btnBrief setImage:[UIImage imageNamed:@"detailBriefUp"] forState:UIControlStateHighlighted];
}
可以通过setNumberOfLines:0,来让概述的所以内容显示出来,但如果,不计算所要显示全部内容的总高度,label的宽高是不会自动增大的。
else{
[lblBrief setNumberOfLines:0];
lblBrief.text = strBrief;
CGSize constraint = CGSizeMake(self.view.frame.size.width - (15 * 2), 20000.0f);
CGSize size = [strBrief sizeWithFont:[UIFont systemFontOfSize:10]constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[brief setFrame:CGRectMake(15,160,size.width,size.height)];
[btnBrief setImage:[UIImage imageNamed:@"detailBriefUp"]forState:UIControlStateNormal];
[btnBrief setImage:[UIImage imageNamed:@"detailBriefUp"]forState:UIControlStateHighlighted];
}
1、创建文件
.h中
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title
font:(UIFont*)font;
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font;
.m中
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title
font:(UIFont *)font
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width,
0)];
label.text = title;
label.font = font;
label.numberOfLines = 0;
[label sizeToFit];
CGFloat height = label.frame.size.height;
return height;
}
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000,
0)];
label.text = title;
label.font = font;
[label sizeToFit];
return label.frame.size.width;
}
2、使用时添加头文件即可
#pragma mark - labelOne SizeToFitHeight
- (void)buildLabelOne
{
UILabel *labelOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 30,
200,
50)];
labelOne.text =
@"这是labelOne的高度自适应这是labelOne的高度自适应这是labelOne的高度自适应这是labelOne的高度自适应";
labelOne.backgroundColor = [UIColor grayColor];
labelOne.font = [UIFont systemFontOfSize:20];
labelOne.numberOfLines = 0;
CGFloat height = [UILabel getHeightByWidth:labelOne.frame.size.width
title:labelOne.text font:labelOne.font];
labelOne.frame = CGRectMake(10, 30, 200, height);
[self.view addSubview:labelOne];
}
#pragma mark - labelTwo SizeToFitWidth
- (void)buildLabelTwo
{
UILabel *labelTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 300,
50,
100)];
labelTwo.text = @"自适应label";
labelTwo.backgroundColor = [UIColor cyanColor];
labelTwo.font = [UIFont systemFontOfSize:20];
CGFloat width = [UILabel getWidthWithTitle:labelTwo.text
font:labelTwo.font];
labelTwo.frame = CGRectMake(10, 300, width, 100);
[self.view addSubview:labelTwo];
}
希望对你有所帮助,不明白的可以追问