2012-08-16 66 views
8

我的文字是縱向模式下的兩行。當我切換到橫向模式時,它適合於一條線。我通過故事板使用靜態tableview單元格;我怎樣才能調整行以適合貼合?具有包裝標籤的靜態表格單元的動態高度?

屏幕是登錄屏幕。

  • 第一個單元格中包含一些說明文字
  • 第二個是一個文本字段,輸入帳戶名
  • 第三個是一個安全的文本字段中輸入密碼
  • 第四個(也是最後一次)單元格包含登錄按鈕。鍵盤上的回車鍵提交表單或切換適當的焦點
+0

檢查斯威夫特一個更好的解決方案 http://stackoverflow.com/以下問題/ 30450434 /基於uilabel的數字大小 - 基於字符串的快速 – David 2015-10-05 07:59:05

+0

不是同一個問題。 – 2016-04-27 00:40:50

回答

9

使用UITableView's heightForRowAtIndexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int topPadding = 10; 
    int bottomPadding = 10; 
    float landscapeWidth = 400; 
    float portraitWidth = 300; 

    UIFont *font = [UIFont fontWithName:@"Arial" size:22]; 

    //This is for first cell only if you want for all then remove below condition 
    if (indexPath.row == 0) // for cell with dynamic height 
    { 
     NSString *strText = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label 
    if(landscape)//depends on orientation 
    { 
     CGSize maximumSize = CGSizeMake(landscapeWidth, MAXFLOAT); // change width and height to your requirement 
    } 
    else //protrait 
    { 
     CGSize maximumSize = CGSizeMake(portraitWidth, MAXFLOAT); // change width and height to your requirement 
    } 

    //dynamic height of string depending on given width to fit 
    CGSize textSize = CGSizeZero; 
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0") 
    { 
     NSMutableParagraphStyle *pstyle = [NSMutableParagraphStyle new]; 
     pstyle.lineBreakMode = NSLineBreakByWordWrapping; 

     textSize = [[strText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :font,NSParagraphStyleAttributeName:[pstyle copy]} context:nil] size]; 
    } 
    else // < (iOS 7.0) 
    { 
     textSize = [strText sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping] 
    } 

    return (topPadding+textSize.height+bottomPadding) // caculate on your bases as u have string height 
    } 
    else 
    { 
     // return height from the storyboard 
     return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
    } 
} 

編輯:增加了對support> and < ios7sizeWithFont方法中的iOS 7.0

棄用
+0

如果我這樣做,我需要手動控制每個單元的高度,而不僅僅是我的第一個,正確的? (這不是很糟糕,我只想說清楚。) – 2012-08-16 05:28:55

+0

檢查已編輯的答案。 – 2012-08-16 05:35:20

+0

謝謝。我認爲這個調用不會與故事板+靜態單元格一起工作,但我已經檢查過,而且你是完全正確的。 – 2012-08-16 05:39:51

8

我已經成功了一個簡單的實現。只要你的靜態表視圖對細胞的適當約束,你可以要求系統的大小爲你:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    let cell = self.tableView(self.tableView, cellForRowAtIndexPath: indexPath) 
    let height = ceil(cell.systemLayoutSizeFittingSize(CGSizeMake(self.tableView.bounds.size.width, 1), withHorizontalFittingPriority: 1000, verticalFittingPriority: 1).height) 
    return height 
} 
+1

這是一個非常古老的問題在現代iOS中,如果您的約束是正確的,添加一個返回'UITableViewAutomaticDimension'的估計函數,你甚至不需要寫高度方法。:) – 2015-11-19 19:58:09

+2

這個答案可能仍然相關。我遇到了iOS 9中的實例,其中具有適當約束的靜態表視圖在更改標籤的文本後不會動態調整大小。上面的解決方法解決了這個問題,而UITableViewAutomaticDimension沒有。 – 2015-11-29 21:46:56

+0

這很有趣,謝謝。 :) – 2015-12-05 17:50:01