计算文本高度

 //计算文本的高度 commentLabel定义的成员变量

int version=[[UIDevice currentDevice].systemVersion intValue];

    NSLog(@"当前系统的版本:%d",version);

    //评论Label的适配

    NSString *commendStr=[NSString stringWithFormat:@"%d条评论",[model.comments intValue]];

    if (version>7) {

        //根据内容的多少调整label的大小(新的API,用来替换下面的)

        CGSize size=[commendStr boundingRectWithSize:CGSizeMake(MAXFLOAT, self.commentLabel.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.commentLabel.font} context:nil].size;

        self.commentLabel.text=commendStr;

        self.commentLabel.frame=CGRectMake(self.commentLabel.x, self.commentLabel.y,size.width, self.commentLabel.height);

        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(@"版本较低");

    }

posted @ 2018-02-24 17:28  姜晓延  阅读(120)  评论(0编辑  收藏  举报