簡體   English   中英

在 tableView 單元格中部分顯示標題?

[英]Partially displaying the Title in the tableView Cell?

我試圖在它顯示的表格視圖單元格中顯示標題數據但部分(...)我想在表格單元格中顯示完整標題可能在兩行或三行我使用自定義單元格並使用 label 並設置 label屬性,但仍部分顯示數據

 titleLabel.text = aNewsInfo.title;

任何人都可以建議我克服這個問題的方法...

提前致謝...

試試這個代碼:

myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
myLabel.text = @"Lorem ipsum dolor sit\namet...";

也許你可以試試:

[titleLabel sizeToFit];

您需要使用以下方法調整單元格的高度:

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

需要設置標簽的屬性

adjustsFontSizeToFitWidth
A Boolean value indicating whether the font size should be reduced in order to fit 
the title string into the label’s bounding rectangle.

@property(nonatomic) BOOL adjustsFontSizeToFitWidth

numberOfLines
The maximum number of lines to use for rendering text.

@property(nonatomic) NSInteger numberOfLines
Discussion
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.

If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.

When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

您好嘗試在您的 cellforRow 方法中添加 label

CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];;
    commentsTextLabel.tag =50;
    [commentsTextLabel setNumberOfLines:0];
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    NSString *text=[@"dasdasdasdasfasfasfasfasfasfsafasfsafasfasfasfasfsafsafasfasfsaf"];
    [commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
    [cell.contentView addSubview:commentsTextLabel];
    [commentsTextLabel release];

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

{
    CGSize labelsize;
    UILabel * textDesc1 = [[UILabel alloc] init];
    [textDesc1 setNumberOfLines:0];
    textDesc1.text=[@"dasdasdasdasfasfasfasfasfasfsafasfsafasfasfasfasfsafsafasfasfsaf"];
    [textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
    labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    labelsize.height=labelsize.height+35;
    [textDesc1 release];
    return (CGFloat)labelsize.height; 


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM