如何在iphone UITableView中根据我的文本长度调整单元格标签的大小?

如何在iphone UITableView中根据我的文本长度调整单元格标签的大小?,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我已经冲浪了,但没有按照我的要求使用任何解决方案 让我先解释一下!我想根据我的文本字符串调整标签的大小!我已经提到了一些问题 现在看,我的第一个字符串是“Hello first”,这个字符串完全适合单元格 现在是第二个字符串的问题。第二个字符串是“你好,第二位,你好吗?一切都好吗?一切都好吗?”根据单元格内容,这是一个太长的字符串。所以我想让我的单元格可以根据字符串文本调整大小 我该怎么办 - (CGFloat)tableView:(UITableView *)tableView heightF

我已经冲浪了,但没有按照我的要求使用任何解决方案

让我先解释一下!我想根据我的文本字符串调整标签的大小!我已经提到了一些问题

现在看,我的第一个字符串是“Hello first”,这个字符串完全适合单元格

现在是第二个字符串的问题。第二个字符串是“你好,第二位,你好吗?一切都好吗?一切都好吗?”根据单元格内容,这是一个太长的字符串。所以我想让我的单元格可以根据字符串文本调整大小

我该怎么办

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
在上面的函数中,您应该计算单元的高度

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
在这里,您应该将适当的frame和numberOfLines设置为零,以便textLabel容纳长字符串

     CGSize maximumSize = CGSizeMake(3000,3000);
     UIFont *dateFont = [UIFont fontWithName:@"Arial" size:16];
     CGSize dateStringSize = ["YOURString" sizeWithFont:dateFont 
                               constrainedToSize:maximumSize 
                                   lineBreakMode:"Yourlable".lineBreakMode];
在上面的函数中,您应该计算单元的高度

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
在这里,您应该将适当的frame和numberOfLines设置为零,以便textLabel容纳长字符串

     CGSize maximumSize = CGSizeMake(3000,3000);
     UIFont *dateFont = [UIFont fontWithName:@"Arial" size:16];
     CGSize dateStringSize = ["YOURString" sizeWithFont:dateFont 
                               constrainedToSize:maximumSize 
                                   lineBreakMode:"Yourlable".lineBreakMode];
它将给出“宽度和高度”。根据高度,您将在表视图单元格中为行索引路径确定“高度(通过动态)

它将给出“宽度和高度”。根据高度,您将在表视图单元格中为行索引路径
确定“高度(通过动态)


您可以将其设置为最大值(如您所想),然后设置标签的numberOfLines属性,而不必设置标签的动态大小。当它到达标签的末尾时,将允许并移动到下一行。还有一件事要设置合适的细胞大小。只有您知道数据的确切长度。

您可以将其设置为最大值(如您所想),然后设置标签的numberOfLines属性,而不是设置标签的动态大小。当它到达标签的末尾时,将允许并移动到下一行。还有一件事要设置合适的细胞大小。只有您知道数据的确切长度。

您需要实现此委托方法
高度为rowatinexpath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //Load text in NSString which you want in Cell's LabelText.
   NSString *cellText=[valueArray objectAtIndex:indexPath.row];

   //define font for Labeltext...
   UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];

   CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);

  //sizeWithFont: Returns the size of the string if it were rendered with the specified constraints. So it will break your line according to font size and constraint size.

   CGSize labelSize_val = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

   return  labelSize_val.height+20;  // Add 20 in height so your UITableView will be neat and clean.

}
注:


–iOS 7中不推荐使用sizeWithFont:constrainedToSize:lineBreakMode:
。改用boundingrect with size:options:attributes:context:)

您需要实现此委托方法
heightForRowAtIndexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //Load text in NSString which you want in Cell's LabelText.
   NSString *cellText=[valueArray objectAtIndex:indexPath.row];

   //define font for Labeltext...
   UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];

   CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT);

  //sizeWithFont: Returns the size of the string if it were rendered with the specified constraints. So it will break your line according to font size and constraint size.

   CGSize labelSize_val = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

   return  labelSize_val.height+20;  // Add 20 in height so your UITableView will be neat and clean.

}
注:

–iOS 7中不推荐使用sizeWithFont:constrainedToSize:lineBreakMode:
。改为使用boundingRectWithSize:options:attributes:context:)