Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting text with setupText or setTagText (after adding the tag to tag list) changes the size of the tag #66

Open
bbhagat opened this issue Dec 8, 2017 · 4 comments

Comments

@bbhagat
Copy link

bbhagat commented Dec 8, 2017

Lets say I have a below setup:

  • An AMTagListView named as tagListView.
  • An AMTagView named as tag1. The text for the tag is "Custom" and it has an accessory image. Below is the code for tag creation and adding it to tagListView:
AMTagView *tag1 = [[AMTagView alloc] initWithFrame:CGRectZero];
[tag1 setAccessoryImage:[UIImage imageNamed:@"close"]];
[tag1 setupWithText:@"Custom"];
[self.tagListView addTagView:tag1];

So when the tag list view shows up on screen, the frame of the tag1: {{10, 39}, {82, 29}}

I wrote a tap handler which does nothing but just resets the text to same text. Code below:

[self.tagListView setTapHandler:^(AMTagView *view) {
     [view setTagText:[NSString stringWithFormat:@"%@", view.tagText]];
}];

If I check the frame after setTagText, it shows up as {{211, 0}, {106, 30}}

This happens because when we use addTagView to add tag to the tagListView, the addTagView method recalculates the width of the tag as shown below:

- (UIView<AMTag> *)addTagView:(UIView<AMTag> *)tagView andRearrange:(BOOL)rearrange {
    if ([tagView isKindOfClass:[AMTagView class]]) {
        UIFont *font = [[[tagView class] appearance] textFont];
        CGSize size = [((AMTagView *)tagView).tagText sizeWithAttributes:@{NSFontAttributeName: font}];
        CGPoint padding = [[[tagView class] appearance] textPadding];
        float tagLength = [[[tagView class] appearance] tagLength];
        size.width = (int)size.width + padding.x * 2 + tagLength;
        size.height = (int)size.height + padding.y;
        size.width = MIN(size.width, self.frame.size.width - self.marginX * 2);
        tagView.frame = (CGRect){{0, 0}, {size.width, size.height}};`
    }
[self.tags addObject:tagView];

We use setupText: or setTagText: to modify the text for the tag that has already been added. Since the tag has already been added, we don't use addTagView: method and hence the above calculation doesn't happen.

As a workaround, I am adding and removing the tag after modifying the text so that it maintains the same padding.

Is this the expected behavior?

@andreamazz
Copy link
Owner

Does the order of the tag change? Have you tried to trigger a rearrange with rearrangeTags on the listview?

@bbhagat
Copy link
Author

bbhagat commented Dec 11, 2017

I am not changing the order of the tags. I just change the text of the tag on tap. For sample purpose, I didn’t even change the text length. I use the exact same text that it already has and I still see the frames changing and tag becoming a little more wider. I did try calling rearrange but it doesn’t help.

I have added a gif that shows the problem (be patient with the gif)

1

@andreamazz
Copy link
Owner

Ok, I see the problem, when the tag is rendered the second time, it takes the options set in the appearance proxy. It happens in the demo too, if you tap the other tags they all inherit the X icon, since it was set in the appearance proxy last.

@andreamazz
Copy link
Owner

Ok, I've made some changes in master, no release for now. Can you test it now?
Please note that every change in the appearance proxy reflects on every tag, so if you need a tag to look different use this way:
https://github.com/andreamazz/AMTagListView/blob/master/TagListViewDemo/TagListViewDemo/AMViewController.m#L43-L44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants