?/** * 传入UILabel控件 然后进行自适应高度 * * @param lbl 控件 */ -(CGFloat)suitable:(UILabel*)lbl{ CGRect originalFrame = lbl.frame; CGRect realFrame =[lbl.text boundingRectWithSize:CGSizeMake(lbl.bounds.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:lbl.font} context:nil]; CGFloat allHeight = 0; //判断该控件的高度是否需要进行调整 if (realFrame.size.height > originalFrame.size.height) { //调整适应后的frame realFrame.origin = originalFrame.origin; lbl.frame = realFrame; //计算调整前后高度的变化 CGFloat compareHeight = realFrame.size.height - originalFrame.size.height; //获取该控件下的所有子控件 并且进行循环 判断如果其子控件是位于该控件的下方的 则需要进行y轴偏移量的调整 调整大小为高度变化之差 NSArray* arr = lbl.superview.subviews; for (UIView *subview in arr) { allHeight += subview.bounds.size.height; if (subview.frame.origin.y > originalFrame.origin.y) { CGRect subviewFrame = subview.frame; subviewFrame.origin.y += compareHeight; subview.frame = subviewFrame; } } } return allHeight; }
原文:http://my.oschina.net/zhangjie9142/blog/513134