diff --git a/AMTagListView/AMTagListView.h b/AMTagListView/AMTagListView.h index 987b34a..15dbe31 100644 --- a/AMTagListView/AMTagListView.h +++ b/AMTagListView/AMTagListView.h @@ -12,6 +12,11 @@ @class AMTagView; @class AMTagListView; +typedef NS_ENUM(NSInteger, AMTagAlignment) { + AMTagAlignmentLeft, + AMTagAlignmentRight +}; + @protocol AMTagListDelegate - (BOOL)tagList:(AMTagListView *)tagListView shouldAddTagWithText:(NSString *)text resultingContentSize:(CGSize)size; @@ -136,4 +141,10 @@ typedef void (^AMTagListViewTapHandler)(AMTagView*); */ @property (nonatomic, assign) id 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 diff --git a/AMTagListView/AMTagListView.m b/AMTagListView/AMTagListView.m index b4b8fd5..19e4824 100644 --- a/AMTagListView/AMTagListView.m +++ b/AMTagListView/AMTagListView.m @@ -40,6 +40,7 @@ - (void)setup { // Default margins _marginX = 4; _marginY = 4; + _tagAlignment = AMTagAlignmentLeft; self.clipsToBounds = YES; _tags = [@[] mutableCopy]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; @@ -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)]; } diff --git a/TagListViewDemo/TagListViewTests/ReferenceImages/AMTagListViewSpec/visuals_looks_right_with_an_array_of_tags_aligned_to_the_right@2x.png b/TagListViewDemo/TagListViewTests/ReferenceImages/AMTagListViewSpec/visuals_looks_right_with_an_array_of_tags_aligned_to_the_right@2x.png new file mode 100644 index 0000000..6238844 Binary files /dev/null and b/TagListViewDemo/TagListViewTests/ReferenceImages/AMTagListViewSpec/visuals_looks_right_with_an_array_of_tags_aligned_to_the_right@2x.png differ diff --git a/TagListViewDemo/TagListViewTests/TagListViewTests.m b/TagListViewDemo/TagListViewTests/TagListViewTests.m index 04969ca..1e693f0 100644 --- a/TagListViewDemo/TagListViewTests/TagListViewTests.m +++ b/TagListViewDemo/TagListViewTests/TagListViewTests.m @@ -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