2012-03-17 43 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已經過時屬性:背景:代替)

相關問題