Zac24
Zac24

Reputation: 2512

How to implement Tags

I'm developing an application where i need to show the tags for every discussion and every comment as shown in image below. I'm getting the tags from service something like this.

tags =             (
                Registration,
                Apps,
                PublicSpeaking,
                Marketing,
                Sales
            );

I want these tags to display under "tag" part of the given image.

enter image description here

this is how i was trying to implement, any kind of tutorial or link will be very helpful.

for (int i=0; i< [self.menuListArray count]; i++) {
    NSString *tag_string = [self.menuListArray objectAtIndex:i];
    CGSize tagSize = [tag_string sizeWithFont:[UIFont fontWithName:@"Roboto-Light" size:13.0]
                         constrainedToSize:CGSizeMake(230, FLT_MAX)
                             lineBreakMode:UILineBreakModeTailTruncation];
    UIImageView *tag_bgimage = [[UIImageView alloc]init];
    [tag_bgimage setFrame:CGRectMake(i*tagSize.width+5, stringSize.height+5, tagSize.width+4, 25)];
    [tag_bgimage setBackgroundColor:[UIColor grayColor]];
    [tag_bgimage setUserInteractionEnabled:NO];

    UILabel *tagLabel = [[UILabel alloc]init];
    [tagLabel setFrame:CGRectMake(0, 0, tagSize.width, 25)];
    [tagLabel setBackgroundColor:[UIColor clearColor]];
    [tagLabel setText:tag_string];
    [tagLabel setFont:[UIFont fontWithName:@"Roboto-Light" size:12.0]];
    [tagLabel setTextAlignment:NSTextAlignmentCenter];

    [tag_bgimage addSubview:tagLabel];
    [cell addSubview:tag_bgimage];

}

Upvotes: 0

Views: 497

Answers (2)

Rahul Kavungal
Rahul Kavungal

Reputation: 91

Instead of using addition framework, I recommend UICollectionView to implement Tags. It's less code, less overhead and easy to implement.

Follow this link for implementing:

https://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/

This is how it looks like. Please note it is flexible too.

enter image description here

Hope this will help you

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25907

You can use this controller. That looks like this:

enter image description here

Upvotes: 1

Related Questions