2012-03-17 42 views
0

我冲浪但没有按照我的要求工作任何解决方案如何在iphone UITableView中根据我的文本长度调整单元格标签的大小?

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

现在看我的第一个字符串为“Hello First”,这个字符串完全符合单元格。

现在第二个字符串的问题。第二个字符串是“你好,第二,你好吗?万物都行吗?在那里?这是根据单元格内容过长的字符串。所以我想让我的单元格按照字符串文本调整大小!

我该怎么办?

回答

-1
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 

在上面的函数中,您应该计算单元格的高度。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

在这里,你应该设置适当的框架和numberOfLines至零为textLabel以适应长字符串。

+0

这不是他问的问题的答案! – 2013-04-23 10:05:55

2
 CGSize maximumSize = CGSizeMake(3000,3000); 
    UIFont *dateFont = [UIFont fontWithName:@"Arial" size:16]; 
    CGSize dateStringSize = ["YOURString" sizeWithFont:dateFont 
           constrainedToSize:maximumSize 
            lineBreakMode:"Yourlable".lineBreakMode]; 

它会给“宽度和高度”每高度的。正如你会在表视图细胞修复你的“heightForRowAtIndexpath”(通过动态)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return dateStringSize.height; 
    } 
         (or)else 
     "set your lable height according to string height" 
+0

雅,好的解决方案Mr.Nag,它会正常工作.... – 2012-03-17 11:29:34

0

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

2

你需要实现这个委托方法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. 

} 

注:选项:

– sizeWithFont:constrainedToSize:lineBreakMode: iOS中7.使用boundingRectWithSize已经过时属性:背景:代替)

相关问题