Skip to content

Commit

Permalink
Add tagAlignment property
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Jul 23, 2015
1 parent 89812b4 commit 753347d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions AMTagListView/AMTagListView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
@class AMTagView;
@class AMTagListView;

typedef NS_ENUM(NSInteger, AMTagAlignment) {
AMTagAlignmentLeft,
AMTagAlignmentRight
};

@protocol AMTagListDelegate <NSObject>

- (BOOL)tagList:(AMTagListView *)tagListView shouldAddTagWithText:(NSString *)text resultingContentSize:(CGSize)size;
Expand Down Expand Up @@ -136,4 +141,10 @@ typedef void (^AMTagListViewTapHandler)(AMTagView*);
*/
@property (nonatomic, assign) id<AMTagListDelegate> tagListDelegate;

/** Tag list alignment
*
* The tag list alignment. The tags can be aligned to the left or the right.
*/
@property (nonatomic, assign) AMTagAlignment tagAlignment;

@end
7 changes: 7 additions & 0 deletions AMTagListView/AMTagListView.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ - (void)setup {
// Default margins
_marginX = 4;
_marginY = 4;
_tagAlignment = AMTagAlignmentLeft;
self.clipsToBounds = YES;
_tags = [@[] mutableCopy];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
Expand Down Expand Up @@ -175,6 +176,12 @@ - (void)rearrangeTags {
[self addSubview:obj];
};

if (self.tagAlignment == AMTagAlignmentRight) {
for (AMTagView *obj in self.tags) {
obj.frame = CGRectMake(self.frame.size.width - obj.frame.origin.x - obj.frame.size.width, obj.frame.origin.y, obj.frame.size.width, obj.frame.size.height);
}
}

[self setContentSize:CGSizeMake(self.frame.size.width, maxY + size.height + self.marginY)];
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions TagListViewDemo/TagListViewTests/TagListViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ - (BOOL)tagList:(AMTagListView *)tagListView shouldAddTagWithText:(NSString *)te
expect(subject).to.haveValidSnapshot();
});

it(@"looks right with an array of tags aligned to the right", ^{
subject = [[AMTagListView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
subject.tagAlignment = AMTagAlignmentRight;
[subject addTags:@[@"Hello", @"World", @"OK?"]];
expect(subject).to.haveValidSnapshot();
});
});

SpecEnd

0 comments on commit 753347d

Please sign in to comment.