根据文本调整单元格宽度-UICollectionView-iOS

根据文本调整单元格宽度-UICollectionView-iOS,ios,objective-c,uicollectionview,width,cgsize,Ios,Objective C,Uicollectionview,Width,Cgsize,我有一个iOS应用程序,带有UICollectionView,它水平显示单元格。在每个单元格中,我都添加了一个简单的标签(目前)。这些标签显示的是名字,有时名字很短,这很好。。。但是有时名称很长,因此会被截断,因为它们不能正确地适应集合视图单元格的宽度 有没有办法动态调整单元格/标签的宽度,以便正确显示名称文本 我正在用这种方法进行实验: -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollection

我有一个iOS应用程序,带有
UICollectionView
,它水平显示单元格。在每个单元格中,我都添加了一个简单的标签(目前)。这些标签显示的是名字,有时名字很短,这很好。。。但是有时名称很长,因此会被截断,因为它们不能正确地适应集合视图单元格的宽度

有没有办法动态调整单元格/标签的宽度,以便正确显示名称文本

我正在用这种方法进行实验:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(106.f, 60.f);
}
但我对上述方法有两个主要问题:

  • 如何从此方法访问单元格标签?(这样我就可以得到它的高度)
  • 我如何计算单元格/标签的实际宽度
  • 谢谢你抽出时间


    Dan

    您无法访问标签,但可以访问用于填充标签的数据。这样,您就可以获得标签的高度和宽度:

    CGRect labelRect = [text
                    boundingRectWithSize:CGSizeMake(MAX WIDTH OF YOUR LABEL, CGFLOAT_MAX)
                    options:NSStringDrawingUsesLineFragmentOrigin
                    attributes:@{
                     NSFontAttributeName : [FONT OF YOUR LABEL]
                    }
                    context:nil];
    

    我在
    TableView
    中也做了同样的事情-希望您可以将此逻辑用于
    collectionView

    celllabelWidth - width of the Cell, you can get it from cellForItemAtIndexPath
    celllabelWidth = cell.lblEvent.frame.size.width;
    
    - (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
            CGSize size = CGSizeZero;
            if (text) {
                //iOS 7
                CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
                size = CGSizeMake(frame.size.width, frame.size.height);
            }
            return size.height;
        }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGFloat heightOfRow = 86.0;
        NSString *yourBlog = [[self.arrevents objectAtIndex:indexPath.row ] valueForKey:@"info"];
        heightOfBlog = [self findHeightForText:yourBlog havingWidth:celllabelWidth andFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];
        heightOfRow += heightOfBlog + 10;
    (long)indexPath.row,heightOfRow);
        return heightOfRow;
    }
    

    希望一切顺利:)

    我在“收藏”视图中用以下代码做了同样的事情:

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        CGSize size1 = CGSizeMake(MAX_WIDTH_OF_STRING, [self height:YOUR_STRING];
        return size1;
    }
    
    -(float)height :(NSString*)string
        {
            UIFont *newFont = [[Util sharedUtil] fontOfType:FONT_HELVETICA_LT bold:NO size:INTERFACE_IS_PHONE?12:FONT_SIZE_LARGE_CELL];
    
            NSDictionary *arialdict = [NSDictionary dictionaryWithObject:newFont forKey:NSFontAttributeName];
    
            NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:string attributes:arialdict];
    
            NSAttributedString *attributedText = message;
            CGRect rect = [attributedText boundingRectWithSize:(CGSize)     
                           {INTERFACE_IS_PHONE?135:220, MAXFLOAT}
                           options:NSStringDrawingUsesLineFragmentOrigin
                            context:nil];
                //you need to specify the some width, height will be calculated
                CGSize requiredSize = rect.size;
            return MAX(requiredSize.height,INTERFACE_IS_PHONE?20:30);
        }
    

    希望它能对您有所帮助。

    感谢大家的发帖,最终对我有效的解决方案是您的所有解决方案和一点额外的“修补”:


    试试这个链接这对我不起作用,它只是崩溃了。另外,对于
    [FONT OF YOUR LABEL]
    它是否需要
    UIFont
    对象?事实上,这对我现在确实有效。但我一直在努力让它发挥作用。有时它提供的宽度有点太大。
    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        // Get the user name and profile name strings.
        NSString *user_label = [NSString stringWithFormat:@"%@", [user_data[indexPath.row] objectAtIndex:0]];
        NSString *name_label = [NSString stringWithFormat:@"%@", [user_data[indexPath.row] objectAtIndex:1]];
    
        // Calculate the contact text widths.
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"SFUIDisplay-Thin" size:20.0f]};
        CGRect rect = [user_label boundingRectWithSize:CGSizeMake(MAXFLOAT, 60) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
        CGRect rect_two = [name_label boundingRectWithSize:CGSizeMake(MAXFLOAT, 60) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    
        // Add the other cell objects width to
        // the biggest calculated text width.
        CGFloat row_width = 0;
    
        if (rect.size.width > rect_two.size.width) {
            row_width = (rect.size.width + 55);
        }
    
        else {
            row_width = (rect_two.size.width + 55);
        }
    
        // Only return the generated width if
        // it is bigger than the original width.
    
        if (row_width > 106) {
            return CGSizeMake(row_width, 60.f);
        }
    
        else {
            return CGSizeMake(106, 60.f);
        }
    }