andreamazz / andreamazz/AMTagListView

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

Offen
#66 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Objective-C
Sterne
757
Forks
112
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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:

```objc
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:

```objc
[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:

```objc
- (UIView *)addTagView:(UIView *)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?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.