【问题标题】:How to make Dynamic height Of UIlable in UItablview Cell in iOS 7 using XCode6如何使用 XCode 6 在 iOS 7 中的 UItableviewCell 中制作 UIlabel 的动态高度
【发布时间】:2015-02-16 19:50:45
【问题描述】:

我正在使用 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 中提供给它的内容。

【问题讨论】:

  • 你设置lbl1.numberofline = 0了吗?
  • 这是为增长标签设置零行数的基本思路。
  • @Rahulgupta:检查我的答案
  • @Rahulgupta 你的问题是标签高度在增加但没有增加对吗?
  • 你能把你得到的截图分享给我吗?因为通过将标签线分配给 0 本身会动态改变标签高度。您是在寻找标签高度还是单元格高度。

标签: ios iphone uitableview ios7 autolayout


【解决方案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 作为额外的填充。 希望这对你有用。

【讨论】:

  • 不适用于在 iOS7 中使用 Xcode6 创建的项目
【解决方案2】:
// 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;

【讨论】:

  • 您是否尝试过使用 iOS7 在 XCode 6 中创建的项目的上述方法?
  • @Rahulgupta:你试过了吗?这是给你的,不是给我的。 :)
  • 感谢您的回复。我已经尝试过上面的代码,但似乎它不适用于 iOS 7,它是在 Xcode6 上创建并启用自动布局的。
  • 如果你使用自动布局,你也应该给出约束。其他明智的使用自动调整大小。
【解决方案3】:
+(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;
}

这里您必须提供默认高度或宽度以及标签文本字体。还将 This 设置为您的 TableView heightForRowAtIndexPath 方法..

【讨论】:

  • 已经实现及以上的 findTextHeight 方法对于自动布局实现来说没有用。
  • 我认为在 iOS7 中使用 Autolayout 提供了一种方法,我们不需要显式计算。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多