如何使用XCode 6在iOS 7中的UItableviewCell中创建UIlabel的动态高度

时间:2014-12-18 05:06:05

标签: ios iphone uitableview ios7 autolayout

我正在使用Xcode 6并创建了一个新项目,并在iOS7中遇到autolayout问题,因为它正确地在iOS8中工作。 我使用下面的代码: -

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"cell";

    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    UILabel *lbl1=(UILabel*)[cell.contentView viewWithTag:1];
    lbl1.text = @"test fkdjsjk kjhsdf ksjh ksjdhf jks kjh kjdhf kjsd h kjh j ,jhfjdfshfk jh fkjsdh fkjs dhkfjhs dkjfh dksjhf jksd hfjkdhs fk jksdhf kjsdh fkjsdh fjkhdsk fjkdsh fkjdsh fkjdsh fkjs djkf djksf kjdshfkjds fkjdsh fkjhs dkjfh kdjs fkdjshfkdsjfh kdjsh fk";


       [lbl1 setPreferredMaxLayoutWidth:lbl1.frame.size.width];




    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    cell.frame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(cell.bounds), CGRectGetHeight(tableView.bounds));

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    return height;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"cell";
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    return cell.frame.size.height;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"cell";

    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
      UILabel *lbl1=(UILabel*)[cell.contentView viewWithTag:1];
     lbl1.text = @"test fkdjsjk kjhsdf ksjh ksjdhf jks kjh kjdhf kjsd h kjh j ,jhfjdfshfk jh fkjsdh fkjs dhkfjhs dkjfh dksjhf jksd hfjkdhs fk jksdhf kjsdh fkjsdh fjkhdsk fjkdsh fkjdsh fkjdsh fkjs djkf djksf kjdshfkjds fkjdsh fkjhs dkjfh kdjs fkdjshfkdsjfh kdjsh fk";

     [lbl1 setBackgroundColor:[UIColor greenColor]];

    [cell.contentView setBackgroundColor:[UIColor redColor]];


    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];



    return cell;
}

我的要求只是标签应该作为iOS 7中提供给它的内容动态调整高度。

3 个答案:

答案 0 :(得分:1)

你需要这样做。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGSize constrainedSize = CGSizeMake(self.view.frame.size.width - 30, FLT_MAX);

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:@"HelveticaNeue" size:14], NSFontAttributeName,
                                          nil];

    CGRect requiredHeight = [YourStringObject boundingRectWithSize:constrainedSize
                                                                      options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                                   attributes:attributesDictionary
                                                                      context:nil];

    return requiredHeight.size.height + 20;
}

我使用额外的20来进行额外的填充。 希望这对你有用。

答案 1 :(得分:0)

+(CGSize)findTextHeight:(float)height width:(float)width string:(NSString *)desc font:(UIFont*)font{
    if ([desc isKindOfClass:[NSNull class]]) {
        return 0.0;
    }
    CGRect textRect =[desc boundingRectWithSize:CGSizeMake(width, height)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName:font}
                                        context:nil];
    CGSize size = textRect.size;
    return size;
}

在这里,你必须给出默认的高度或宽度和标签文字字体。同时将此设置为TableView heightForRowAtIndexPath方法..

答案 2 :(得分:0)

// Pass the string value for display on label

self.yourLabel.numberofline = 0 // don't forget to set the number of lines zero
 +(CGSize)heigtForCellwithString:(NSString *)stringValue
   {      
      //For iPhone
       UIFont *font = [UIFont fontWithName:@"Helvetica-Light" size:15];// Here change the font name and font size you have used.
       CGSize constraint = CGSizeMake(320,9999);
       NSDictionary *attributes = @{NSFontAttributeName: font}; 
       CGRect rect = [stringValue boundingRectWithSize:constraint
                                               options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                            attributes:attributes
                                               context:nil];
       return rect.size;
    }

    CGSize labelHeight = [NSString heigtForCellwithString:@"your text"];
    CGRect newFrame =  self.yourLabel.frame;
    newFrame.size.height = labelHeight.height;
    self.yourLabel.frame = newFrame;