Question

I need to create a view like below from the array of emails that I receive which also has the delete option.

enter image description here

Something similar to that of gmail's mail recipient except in my this should be scrollview. My main issue is to create the background with delete button which stretches based on the email length. My current approach is to use 3 images one for the beginning, one for the end with delete button and one in general for middle that will stretch. Is there any other or better way to do this?

NOTE:Need to support from iOS 5 and above

Was it helpful?

Solution

Here you go, I have created a SuperLabel for you though it might need some tweak.. But it will help you for sure..

SuperLabel.h

#import <UIKit/UIKit.h>

@interface SuperLabel : UIView
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
@end

SuperLabel.m

#import "SuperLabel.h"

#define MAX_HEIGHT 25.0

@implementation SuperLabel

- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        //Design your label view
        self.backgroundColor=[UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1.0];
        self.layer.borderColor=[[UIColor orangeColor] CGColor];
        self.layer.borderWidth=1.0;
        self.layer.cornerRadius=5.0;

        //Add a Label
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 100.0, MAX_HEIGHT)];
        label.font=[UIFont systemFontOfSize:12.0];
        label.textColor=[UIColor grayColor];
        label.text=title;
        [label sizeToFit];

        [self addSubview:label];

        //We will get resized frame after using sizeToFit after setting the text in the label.
        CGRect rect=label.frame;

        //Add a button
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"x" forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:12.0];
        btn.titleLabel.textColor=[UIColor grayColor];

        rect.origin.x=rect.origin.x+rect.size.width;
        rect.origin.y=0;
        rect.size=CGSizeMake(25.0, MAX_HEIGHT);

        [self addSubview:btn];

        [btn addTarget:self action:@selector(deleteMe:) forControlEvents:UIControlEventTouchDragInside];

        btn.frame=rect;


        //Change the whole frame of the label view
        frame.size.height=MAX_HEIGHT;
        frame.size.width=btn.frame.origin.x+btn.frame.size.width;

        self.frame=frame;


    }
    return self;
}


-(IBAction)deleteMe:(id)sender{
    [self removeFromSuperview];
}
@end

And finally add to your view by using following code..

    SuperLabel *label=[[SuperLabel alloc] initWithFrame:CGRectZero andTitle:@"myemail@gmail.com"];
    [self.view addSubview:label];

Cheers..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top