簡體   English   中英

如何使用XCode6在iOS 7的UItablview單元格中使UIlable動態高度

[英]How to make Dynamic height Of UIlable in UItablview Cell in iOS 7 using XCode6

我正在使用Xcode 6並創建了一個新項目,並且在iOS7中自動布局出現問題,因為它在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中提供給它的內容作為對象的動態高度。

您需要這樣做。

-(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作為額外的填充。 希望這對你有用。

+(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方法。

// 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM