天天看點

動态計算 UILabel 的高度的代碼

舊方法:

  self.bookInfoRecCentLab.text = summaryStr;

        self.bookInfoRecCentLab.font = 0;

        size1 = [summaryStr sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(bookInfoCentLabW, 900)];

        self.bookInfoRecCentLab.font = [UIFont systemFontOfSize:15];

新方法:

    CGSize size_1=CGSizeMake(280, 200);

    NSString *detailString=@"簡潔打算将開放和大廈開獎号愛的色放空間哈斯短發科技愛的色放離開";

    NSDictionary *dic1=[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];

    _size1=[detailString boundingRectWithSize:size_1 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dic1 context:nil].size;