Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions JNWCollectionView/JNWCollectionViewGridLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

@class JNWCollectionViewGridLayout;

typedef NS_ENUM(NSUInteger, NSCellAlignment) {
NSCellAlignmentLeft = 0, // Visually left aligned
NSCellAlignmentRight = 1, // Visually right aligned
NSCellAlignmentCenter = 2, // Visually centered
};

/// The supplementary view kind identifiers used for the header and the footer.
extern NSString * const JNWCollectionViewGridLayoutHeaderKind;
extern NSString * const JNWCollectionViewGridLayoutFooterKind;
Expand Down Expand Up @@ -77,4 +83,9 @@ extern NSString * const JNWCollectionViewGridLayoutFooterKind;
/// Defaults to 0
@property (nonatomic, assign) CGFloat itemHorizontalMargin;

/// Optional grid cell's horizon alignment, when 'itemPaddingEnabled' was disabled
///
/// Default to NSCellAlignmentLeft
@property (nonatomic, assign) NSCellAlignment cellAlignment;

@end
9 changes: 9 additions & 0 deletions JNWCollectionView/JNWCollectionViewGridLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ - (void)prepareLayout {
}
self.numberOfColumns = numberOfColumns;

CGFloat extraHorizonSpace = 0.f;
if (self.itemPaddingEnabled == NO) {
extraHorizonSpace = totalWidth - (self.numberOfColumns * (itemSize.width + self.itemPadding));
}

CGFloat totalHeight = 0;
for (NSUInteger section = 0; section < numberOfSections; section++) {
NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
Expand All @@ -147,6 +152,10 @@ - (void)prepareLayout {
CGPoint origin = CGPointZero;
NSInteger column = ((item - (item % numberOfColumns)) / numberOfColumns);
origin.x = sectionInsets.left + self.itemPadding + (item % numberOfColumns) * (itemSize.width + self.itemPadding);
if (self.itemPaddingEnabled == NO && self.cellAlignment == NSCellAlignmentCenter)
origin.x += (extraHorizonSpace/2);
else if (self.itemPaddingEnabled == NO && self.cellAlignment == NSCellAlignmentRight)
origin.x += extraHorizonSpace;
origin.y = column * itemSize.height + column * verticalSpacing;
sectionInfo.itemInfo[item].origin = origin;
}
Expand Down
4 changes: 4 additions & 0 deletions demo/JNWCollectionViewDemo/GridDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ - (void)awakeFromNib {
JNWCollectionViewGridLayout *gridLayout = [[JNWCollectionViewGridLayout alloc] init];
gridLayout.delegate = self;
gridLayout.verticalSpacing = 10.f;

// ex) Cell Alignment
//gridLayout.itemPaddingEnabled = NO;
//gridLayout.cellAlignment = NSCellAlignmentCenter;

self.collectionView.collectionViewLayout = gridLayout;
self.collectionView.dataSource = self;
Expand Down