Question

I would like to create a custom class by subclassing the UIView that would look like the one below:
My target

Oh and the text says the body view should be 80px tall, its 50px.
But I cant seem to get it right. I have the class header prepared like so:

#import <UIKit/UIKit.h>

@interface MessageView : UIView

@property (strong, nonatomic)UIImage *backgroundImage;
@property (strong, nonatomic)UITextView *messageView;
@property (strong, nonatomic)UILabel *titleLabel;

- (id)initWithBackgroundImage:(NSString*)background;

- (void)setTitle:(NSString*)titleMessage;
- (void)setBody:(NSString*)bodyMessage;

@end

Now I will create these views by calling initWithBackgroundImage where I will pass the background image file name. The example shows one of these images. How do I implement the init method so that init will create a body like on the example except the texts will be empty?

My futile attempt of the init method was this:

- (id)initWithBackgroundImage:(NSString*)background
{
    self = [super init];//[super initWithFrame:CGRectMake(0, 0, 270, 100)];
    if (self)
    {
        backgroundImage = [[UIImage imageNamed:background] retain];
        messageView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 250, 50)];
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
    }
    return self;
}

But nothing happened. When done I will set the text via setter methods. And I would like to set the position on the final view via setCenter(x, y). I want the class to have "fixed" width and height.

Thanks!

Was it helpful?

Solution

EDIT:

   - (id)initWithBackgroundImage:(NSString*)background
    {
        self = [super init];//[super initWithFrame:CGRectMake(0, 0, 270, 100)];
        if (self)
        {
            UIImageView *bg  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:background]];      
            messageView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 250, 50)];
            titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
            [self addSubview:bg];
            [bg release];
            [self addSubview:messageView];
            [messageView release];
            [self addSubview:titleLabel];
            [titleLabel release];
        }
        return self;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top