2012-02-28 66 views
0

在我的iphone應用程序中,我有一個帶有定製單元格的UITableView。單元格上其中一個標籤的文本可能很長,我希望它可以包裝並剪裁以進行綁定。我嘗試了幾件事,但標籤仍然延伸到附件圖標下方。UILable在定製表格單元格上的奇怪

回答

1

如果您有自定義的UITableViewCell,那麼您可以實現- (void) layoutSubviews方法並調整單元格中的UILabel的大小。

喜歡的東西:

CGRect titleFrame = _title.frame; 
NSString *currentText = [_title text]; 
CGSize size = [currentText sizeWithFont:_title.font constrainedToSize:CGSizeMake(titleFrame.size.width, 50.0f) lineBreakMode:_title.lineBreakMode]; 
titleFrame.size.height = size.height; 
_title.frame = titleFrame; 

哪裏_title是的UILabel。

1

我會添加到elpri的答案,你不需要經歷所有創建UITableViewCell的自定義子類的麻煩。相反,你可以使用標籤,但一般的方法是正確的。

  1. 計算幀的大小,它限制使用calculatedSize.widthcalculatedSize.height

使用標籤,這將看起來像使用的UILabel幀的sizeWithFont:constrainedToSize:lineBreakMode:

  • 設置寬度/高度最大尺寸:

    #define NAMELABEL_TAG 1 
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"ACellIdentifier"; 
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        UILabel *nameLabel = (UILabel *)[cell viewWithTag:NAMELABEL_TAG]; 
        // the rest of your code to resize the frame can go here... 
    } 
    

    此外,請確保在界面生成器中正確設置支柱和彈簧,以使其自動翻轉當設備旋轉時(假設這是你想要的)。