//計算文本的高度 commentLabel定義的成員變量app
int version=[[UIDevice currentDevice].systemVersion intValue];orm
NSLog(@"當前系統的版本:%d",version);string
//評論Label的適配it
NSString *commendStr=[NSString stringWithFormat:@"%d條評論",[model.comments intValue]];io
if (version>7) {變量
//根據內容的多少調整label的大小(新的API,用來替換下面的)model
CGSize size=[commendStr boundingRectWithSize:CGSizeMake(MAXFLOAT, self.commentLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.commentLabel.font} context:nil].size;di
self.commentLabel.text=commendStr;ping
self.commentLabel.frame=CGRectMake(self.commentLabel.x, self.commentLabel.y,size.width, self.commentLabel.height);vi
NSLog(@"版本大於7.0");
}else{
//根據內容的多少調整label的大小(老的API)
CGSize size=[commendStr sizeWithFont:_commentLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, _commentLabel.frame.size.height) lineBreakMode:NSLineBreakByWordWrapping];
_commentLabel.text=commendStr;
_commentLabel.frame=CGRectMake(_commentLabel.x, _commentLabel.y, size.width, _commentLabel.height);
NSLog(@"版本較低");
}