我想通过子类化UIView
来创建一个自定义类,如下所示:
哦,文字说身体视图应该是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)
在最终视图中设置位置。我希望班级有&#34;修复&#34;宽度和高度。
谢谢!
答案 0 :(得分: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;
}