首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于UILabel文本的UICollectionReusableView高度

基于UILabel文本的UICollectionReusableView高度
EN

Stack Overflow用户
提问于 2014-11-05 19:13:14
回答 1查看 1.6K关注 0票数 4

我有一个想要在我的collectionView的标题中显示的UICollectionReusableView。

我为头文件创建了一个XIB文件,并拖动了一个UICollectionReusableView,并使用自动布局在其中布局了元素。可重用视图中有2个标签具有来自服务器的动态内容,因此它们的高度各不相同。

我想在运行时计算动态高度,并从以下位置返回:

代码语言:javascript
复制
collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize

我注意到的是,标题视图的高度始终等于XIB文件中设置的高度。

EN

回答 1

Stack Overflow用户

发布于 2015-02-24 15:06:25

对于目标c和编程内部,视图确实显示为CGRect requiredHeight高度;//创建为变量

代码语言:javascript
复制
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
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26755800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档