【问题标题】:Partially displaying the Title in the tableView Cell?在 tableView 单元格中部分显示标题?
【发布时间】:2011-05-31 07:53:11
【问题描述】:

我试图在它显示的 Tableview 单元格中显示标题数据,但部分(...)我想在表格单元格中显示完整标题可能在两行或三行我使用自定义单元格并使用标签并设置标签属性,但仍部分显示数据

 titleLabel.text = aNewsInfo.title;

谁能建议我克服这个问题的方法...

在此先感谢...

【问题讨论】:

    标签: iphone objective-c xcode4


    【解决方案1】:

    试试这个代码:

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

    【讨论】:

    • 阿曼这将有效,但仅适用于像 cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 这样的硬编码文本cell.textLabel.numberOfLines = 2; cell.textLabel.text = aNewsInfo.title;但在 titleLabel.lineBreakMode = UILineBreakModeWordWrap; titleLabel.numberOfLines = 2; titleLabel.text = aNewsInfo.title;但这在 tableView 单元格中没有给出任何内容,只是空白我应该做什么......
    • 你是否改变了标签的高度,对于动态数据设置 numberoflines=0 如上所述。
    • 是的,我已经将高度 n 全部更改为正确地将 numberofline 设置为 0 但仍然是同样的问题...
    【解决方案2】:

    也许你可以试试:

    [titleLabel sizeToFit];
    

    你需要调整单元格的高度:

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

    【讨论】:

      【解决方案3】:

      需要设置标签的属性

      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.
      

      【讨论】:

      • 因为我使用了自定义单元格,所以我使用 IB 进行操作,我将 Attribute Line 设置为 3 ,并通过拖动边界来增加标签的大小,但现在标题已完全隐藏,仅显示三个点 (. ..)。现在我该怎么办...
      • 您将行数设置为 0 和最小字体大小。
      【解决方案4】:

      您好,尝试在您的 cellforRow 方法中添加标签

      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; 
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多