【问题标题】:Make dynamic UILabel Heights/Widths in UITableViewCell ios?在 UITableViewCell ios 中制作动态 UILabel 高度/宽度?
【发布时间】:2014-04-30 08:20:45
【问题描述】:

我想让标签动态化(当文本大小增加时,高度和宽度会发生变化),在 iOS7 中可以吗?发布完整的教程或任何帖子。 提前致谢。

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    是的。可以这样做:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    
    
    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    ...
    
    int fontSize = 13;
    int height = ROW_HEIGHT_SCALE;
    
    if(height==5){
            fontSize = 10;
        }else if(height>5 && height <100){
            fontSize = 12;
        }else if(height>100 && height <300){
            fontSize = 16;
        }else if(height>300){
            fontSize = 18;
    }
    
    [cell.textLabel setFont:[UIFont fontWithName: @"Arial" size: fontSize]];
    
    ...
    
    return cell;
    }
    

    【讨论】:

      【解决方案2】:
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      { 
        static NSString *cellIdentifier =@"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
        if (!cell) {
            cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
          }
       UILabel *itemNameLabel = (UILabel *)[cell viewWithTag:1];
       itemNameLabel.text = @"Your text";
       CGSize labelSize = [self WidthOfCellWithIngredientLine:itemNameLabel];
       itemNameLabel.frame = CGRectMake(10, 10, 250, labelSize.height);
      
      }
      
      - (CGSize)WidthOfCellWithIngredientLine:(UILabel *)stringLabel
      {
          stringLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15.0f];
          CGSize maximumLabelSize = CGSizeMake(310, 9999);
          CGSize expectedSize = [stringLabel sizeThatFits:maximumLabelSize];
          return expectedSize;
      }
      

      请注意:根据标签高度也设置tableCell高度。

      stringLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15.0f];

      设置您在标签上设置的字体名称和字体大小

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 2012-10-18
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多