Высота UICollectionReusableView на основе текста UILabel

У меня есть UICollectionReusableView, который я хотел бы показать в заголовке моего collectionView.

Я создал файл XIB для заголовка и перетащил UICollectionReusableView и разместил элементы внутри него, используя автоматическую компоновку. 2 метки внутри повторно используемого представления имеют динамический контент, поступающий с сервера, поэтому их высота варьируется.

Я хотел бы рассчитать динамическую высоту во время выполнения и вернуть ее из:

collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize

Что я заметил, так это то, что высота представления заголовка всегда равна высоте, установленной в файле XIB.

введите здесь описание изображения


person Mohit Athwani    schedule 05.11.2014    source источник
comment
Вы когда-нибудь решали это?   -  person Tim Vermeulen    schedule 24.02.2017


Ответы (1)


для цели c и программно внутри представления появился CGRect requiredHeight;//создать как переменную

labelToUseInHeader = [self createLblWithfontStyle:@"thin" fontSize:16 frameDimen:CGRectMake(0, 0,collectionView.frame.size.width-16, 40) andTextColor:UIColorFromRGB(0x000000)];

CGSize constrainedSize = CGSizeMake(labelToUseInHeader.frame.size.width,9999);

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:                                       [UIFont fontWithName:@"HelveticaNeue-Thin" size:16.0], NSFontAttributeName,nil];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:"stringToProcessFromServer" attributes:attributesDictionary];

requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
requiredHeight = CGRectMake(0,0,labelToUseInHeader.frame.size.width, requiredHeight.size.height);

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {

        reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

        if (reusableview==nil)
        {
            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width,200)];
        }

        labelToUseInHeader=[self createLblWithfontStyle:@"thin" fontSize:16 frameDimen:CGRectMake(8,0, collectionView.frame.size.width-16, 40) andTextColor:UIColorFromRGB(0x000000)];
        labelToUseInHeader.numberOfLines=0;
        labelToUseInHeader.userInteractionEnabled=NO;
        labelToUseInHeader.text="string from server";
        [labelToUseInHeader sizeToFit];

        [reusableview addSubview:labelToUseInHeader];
    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ 
return CGSizeMake(collectionView.frame.size.width,requiredHeight.size.height+10);//+10 for padding
 }
person Tanuj Jagoori    schedule 24.02.2015