简体   繁体   English

标签文本增加时,如何以编程方式增加集合视图单元格的高度?

[英]How to programmatically increase collection view cell height when label text increases?

How can I increase the height of the UICollectionViewCell? 如何增加UICollectionViewCell的高度? I have implemented 2 functionalities in my project: 我在项目中实现了2个功能:

- (NSArray *)cellSizes {

/** Here we mentioned the code for size of collection view cells  **/
_cellSizes = [NSMutableArray array];

if ([homePageArray count] != 0 )
{
    _cellSizes = [NSMutableArray array];
    [_cellSizes removeAllObjects];

    for (NSInteger i = 0; i < [homePageArray count]; i++)
    {
        CGSize size;
        UIImageView *sampleImage = [[[UIImageView alloc]init]autorelease];
        [sampleImage setFrame:CGRectMake(0,0,(delegate.windowWidth-30),150)];
        CGSize size1 = [self aspectScaledImageSizeForImageView:sampleImage width:150 height:150];
        //        [productTitleLbl setFont:[UIFont fontWithName:appFontRegular size:14]]; //78 //60
        CGFloat heightOfStr = [self heightForString:[[homePageArray objectAtIndex:i]objectForKey:@"item_title"] font:[UIFont fontWithName:appFontRegular size:14] maxWidth:(delegate.windowWidth-35)];

        size=CGSizeMake(size1.width, size1.height+((heightOfStr>30)?heightOfStr+55:85));
        _cellSizes[i] =[NSValue valueWithCGSize:size];
    }
}
return _cellSizes;
}

And For image I used 对于我使用的图像

- (CGSize) aspectScaledImageSizeForImageView:(UIImageView *)iv width:(float)wid height:(float)hgth
{

float x,y;
float a,b;
x = iv.frame.size.width;
y = iv.frame.size.height;
a = wid;
b = hgth;

if ( x == a && y == b ) {           // image fits exactly, no scaling required
    // return iv.frame.size;
}
else if ( x > a && y > b ) {         // image fits completely within the imageview frame
    if ( x-a > y-b ) {              // image height is limiting factor, scale by height
        a = y/b * a;
        b = y;
    } else {
        b = x/a * b;                // image width is limiting factor, scale by width
        a = x;
    }
}
else if ( x < a && y < b ) {        // image is wider and taller than image view
    if ( a - x > b - y ) {          // height is limiting factor, scale by height
        a = y/b * a;
        b = y;
    } else {                        // width is limiting factor, scale by width
        b = x/a * b;
        a = x;
    }
}
else if ( x < a && y > b ) {        // image is wider than view, scale by width
    b = x/a * b;
    a = x;
}
else if ( x > a && y < b ) {        // image is taller than view, scale by height
    a = y/b * a;
    b = y;
}
else if ( x == a ) {
    a = y/b * a;
    b = y;
} else if ( y == b ) {
    b = x/a * b;
    a = x;
}


return CGSizeMake(a,b);

}

And for string I used 对于我使用的字符串

- (CGFloat)heightForString:(NSString *)text font:(UIFont *)font maxWidth:(CGFloat)maxWidth {
if (![text isKindOfClass:[NSString class]] || !text.length) {
    // no text means no height
    return 0;
}

NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
NSDictionary *attributes = @{ NSFontAttributeName : font };
CGSize size = [text boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX) options:options attributes:attributes context:nil].size;
CGFloat height = ceilf(size.height) + 1; // add 1 point as padding

return height;
}

And in collection view layout 并在集合视图布局中

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

if([homePageArray count]!=0)
    return [self.cellSizes[indexPath.item] CGSizeValue];
else
    return CGSizeMake(0, 0);
}

And I calculated the label function in 我计算了标签功能

CGFloat HeightForString = [self heightForString:[[homePageArray objectAtIndex:indexPath.row]objectForKey:@"item_title"] font:[UIFont fontWithName:appFontRegular size:14] maxWidth:cell.frame.size.width];
        [cell.productTitleLbl setFrame:CGRectMake(10, cell.frame.size.height-60, cell.frame.size.width,(HeightForString>30)?HeightForString:30)];

But the cell is not resizing if the label has lengthy name. 但是,如果标签名称过长,则单元格不会调整大小。 Can you please help me? 你能帮我么?

Try this out or go through the link which I have put on comment. 试试看或通过我发表评论的链接。

 itemTitleText =[[self.jsonData 
 objectAtIndex:indexPath.row]objectForKey:@"item_title"];


 NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[itemTitleText 
 dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: 
 NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber 
 numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];


    CGRect labelRect = [itemTitleText
                        boundingRectWithSize:CGSizeMake(270, CGFLOAT_MAX)
                        options:NSStringDrawingUsesLineFragmentOrigin
                        attributes:@{
                         NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue- 
   Medium" size:14.0]
                                     }
                        context:nil];
    cell.itemTitleText.frame=CGRectMake(cell.itemTitleText.frame.origin.x, 
     cell.itemTitleText.frame.origin.y, cell.itemTitleText.frame.size.width, 
    labelRect.size.height);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用自动布局表格视图时,如何增加表格视图单元格中的标签高度(取决于文本)? - How to increase Label Height(Depend on Text) in Tableview cell when used Autolayout Table view? 当单元格的高度增加时,如何保持单元格标签的文本顶部? - How to keep the text of cell label at top when cell's height increases? 以编程方式更改集合视图单元格中标签的字体 - Change font of label in collection view cell programmatically 在swift 4.0中点击收集视图单元格时更改标签的文本 - Changing the text of a label when a collection view cell is tapped in swift 4.0 如何使用CoreData从选定的集合视图单元格中获取标签文本? - How to Get Label Text from Selected Collection View Cell with CoreData? 如何在表格视图单元格中增加文本标签和详细文本标签之间的空间? - How to increase space between text label and detail text label in table view cell? 如何在swift中根据集合视图单元格内的标签内容自动调整高度? - How to automatically adjust height based on label content inside collection view cell in swift? 如何以编程方式使集合视图单元格高度等于具有最高高度的文本 - How to make collection view cell height equal to the text with the highest height and programatically 如何在iPhone中以编程方式增加UITableView单元格的高度? - How to programmatically increase UITableView cell's height in iPhone? Collection视图是否会增加Cell大小 - Will the Collection view Cell size increase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM