0

我想通过子类化来创建一个自定义类,UIView如下所示:
我的目标

哦,文字说body view应该是80px高,它是50px。
但我似乎无法做到这一点。我有这样的类头准备:

#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

现在我将通过调用initWithBackgroundImage传递背景图像文件名的位置来创建这些视图。该示例显示了这些图像之一。如何实现 init 方法,以便 init 将创建一个类似于示例的主体,但文本将为空?

我对 init 方法的徒劳尝试是这样的:

- (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;
}

但是什么也没发生。完成后,我将通过 setter 方法设置文本。我想通过setCenter(x, y). 我希望班级具有“固定”的宽度和高度。

谢谢!

4

1 回答 1

0

编辑:

   - (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;
}
于 2012-11-12T10:52:57.500 回答