//LTView.h文件
@interface LTView : UIView<UITextFieldDelegate>
@property (nonatomic,retain)UILabel * label;
@property (nonatomic,retain)UITextField * textField;
//自定義初始化方法
-(instancetype)initWithPlaceholder:(NSString *)placeHolder;
//自定義初始化方法(尺寸)
-(instancetype)initWithFrame:(CGRect)frame andPlaceHoder:(NSString *)placeholder;
@end
//LTView.m文件
@implementation LTView
-(void)dealloc{[_label release];
[_textField release];
[super dealloc];
}
-(instancetype)initWithPlaceholder:(NSString *)placeHolder{
CGRect frme = CGRectMake(0, 0, 375, 40);
self = [super initWithFrame:frme];
if (self) {
//label
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 5, frme.size.width/3.0, 30)];
self.label = label;//外界可以訪問
[self addSubview:label];//self指的是視圖對象,因爲減號方法
[label release];
//textfield
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(50+frme.size.width/3.0, 3, frme.size.width/3.0*2, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = placeHolder;
self.textField = textField;
[self addSubview:textField];
[textField release];
}
return self;
}
//自定義初始化方法(尺寸)
-(instancetype)initWithFrame:(CGRect)frame andPlaceHoder:(NSString *)placeholder{
self = [super initWithFrame:frame];
if (self) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, frame.size.width/3.0, 30)];
_label.backgroundColor = [UIColor orangeColor];
[self addSubview:_label];
// [_label release];
_textField = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width/3.0, 5, frame.size.width/3.0*2, 30)];
_textField.placeholder = placeholder;
_textField.borderStyle = UITextBorderStyleLine;
_textField.backgroundColor = [UIColor blueColor];
_textField.delegate = self;
[self addSubview:_textField];
[_textField release];
}
return self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//AppDelegate.m文件
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc{
[_window release];
[super dealloc];
}
- (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];
// LTView * lt1 = [[LTView alloc] initWithPlaceholder:@"請輸入用戶名"];
//
// lt1.label.text = @"用戶名";
// [self.window addSubview:lt1];
// [lt1 release];
LTView * lt2 = [[LTView alloc]initWithFrame:CGRectMake(0, 50, 375, 40) andPlaceHoder:@"用戶"];
lt2.label.text = @"名字";
[self.window addSubview:lt2];
[lt2 release];
LTView * lt3 = [[LTView alloc]initWithFrame:CGRectMake(0, 100, 375, 40) andPlaceHoder:@"密碼"];
lt3.label.text = @"密碼";
//修改顏色
lt3.textField.backgroundColor = [UIColor redColor];
[self.window addSubview:lt3];
[lt3 release];
return YES;
}