2016-09-10 3 views
2

나는 UITextView을 포함하는 CellUICollectionView을 가지고 있습니다. TextView의 높이에 따라 셀의 크기가 달라지기를 원합니다 (스크롤이 필요없고 모든 텍스트가 항상 표시되도록). 나는 이것을 시도하고있다 :컬렉션 셀 크기보기 UITextView에 따라 달라짐

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    if (indexPath.row < myComments!.count) { 

     if let messageText = myComments![indexPath.row].text { 
      let size = CGSizeMake(self.view.frame.width/2, 100) 
      let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin) 
      let estimatedFrame = NSString(string: messageText).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)], context: nil) 
      return CGSizeMake(view.frame.width, estimatedFrame.height + 50) 
     } 
    } 
    return CGSizeMake(view.frame.width, 100) 

} 

그러나 이것은 실제로 작동하지 않는다. 때로는 높이가 너무 길고 때로는 너무 작아서 스크롤해야합니다.

답변

0

TextView의 sizeThatFits 메서드를 사용하십시오. 그것은 UITextView가 필요로하는 정확한 크기를 계산합니다.

관련 문제