一丶
#import "AppDelegate.h"
#import "LTView.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
// 自定义View
LTView *ltView = [[LTView alloc] initWithFrame:CGRectMake(30, 50, 295, 50)];
ltView.myTextField.placeholder = @"请输入用户名";
ltView.myLabel.backgroundColor = [UIColor magentaColor];
[self.window addSubview:ltView];
[ltView release];
return YES;
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
二,自定义View
#import <UIKit/UIKit.h>
@interface LTView : UIView
// 自定义控件一般来说会把自带的视图写成属性,方便外部使用
@property (nonatomic, retain)UILabel *myLabel;
@property (nonatomic, retain)UITextField *myTextField;
@end
#import "LTView.h"
@implementation LTView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 在view的初始化方法中,对所有的子视图进行初始化创建
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)];
self.myLabel.backgroundColor = [UIColor orangeColor];
[self addSubview:self.myLabel];
[_myLabel release];
self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width / 3 + 20, 0, frame.size.width * 2 / 3 - 20, frame.size.height)];
self.myTextField.placeholder = @"请输入";
self.myTextField.borderStyle = UITextBorderStyleRoundedRect;
self.myTextField.clearButtonMode = UITextFieldViewModeAlways;
self.myTextField.backgroundColor = [UIColor whiteColor];
[self addSubview:self.myTextField];
[_myTextField release];
}
return self;
}
- (void)dealloc
{
[_myLabel release];
[_myTextField release];
[super dealloc];
}