计算文本高度

 //计算文本的高度 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(@"版本较低");

    }

相关文章
相关标签/搜索