OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 6 |
| 7 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." |
| 11 #endif |
| 12 |
| 13 @implementation MDCCollectionViewCell (Chrome) |
| 14 |
| 15 + (CGFloat)cr_preferredHeightForWidth:(CGFloat)targetWidth |
| 16 forItem:(CollectionViewItem*)item { |
| 17 // Dictionary where keys are class names and values are sizing cells. |
| 18 static NSMutableDictionary<NSString*, MDCCollectionViewCell*>* |
| 19 gSharedSizingCells; |
| 20 static dispatch_once_t onceToken; |
| 21 dispatch_once(&onceToken, ^{ |
| 22 gSharedSizingCells = [NSMutableDictionary dictionary]; |
| 23 }); |
| 24 |
| 25 // Get the sizing cell for the given class, or create it if needed. |
| 26 NSString* className = NSStringFromClass(item.cellClass); |
| 27 MDCCollectionViewCell* cell = gSharedSizingCells[className]; |
| 28 if (!cell) { |
| 29 cell = [[item.cellClass alloc] init]; |
| 30 // Don't use autoresizing mask constraints, as they will conflict when |
| 31 // laying out the cell. |
| 32 cell.contentView.translatesAutoresizingMaskIntoConstraints = NO; |
| 33 // Store it in the dictionary. |
| 34 gSharedSizingCells[className] = cell; |
| 35 } |
| 36 |
| 37 // Configure the cell with the item. |
| 38 [item configureCell:cell]; |
| 39 return [cell cr_preferredHeightForWidth:targetWidth]; |
| 40 } |
| 41 |
| 42 - (CGFloat)cr_preferredHeightForWidth:(CGFloat)targetWidth { |
| 43 // Set the cell's frame to use the target width. |
| 44 CGRect frame = self.frame; |
| 45 const CGFloat kMaxHeight = 0; |
| 46 frame.size = CGSizeMake(targetWidth, kMaxHeight); |
| 47 self.frame = frame; |
| 48 |
| 49 // Layout the cell. |
| 50 [self setNeedsLayout]; |
| 51 [self layoutIfNeeded]; |
| 52 |
| 53 // Compute how tall the cell needs to be. |
| 54 CGSize computedSize = |
| 55 [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; |
| 56 |
| 57 return computedSize.height; |
| 58 } |
| 59 |
| 60 @end |
OLD | NEW |