客觀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
}
你有沒有解決這個問題? – 2017-02-24 18:47:03