设置IOS的UILabel动态高度方法
2014-11-04来源:

开始的时候按钮事件中,可能写的是这样一段代码:

if (isExpand) {

[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];

}

更多信息请查看IT技术专栏

推荐信息
Baidu
map