AppDelegate.m
//定义LTView类型的对象
LTView * usernameLT = [[LTView alloc] initWithFrame:CGRectMake(20, 100, 280, 60) descriptionText:@"用户名"];
//usernameLT.backgroundColor = [UIColor cyanColor];
[self.window addSubview:usernameLT];
[usernameLT release];
[usernameLT setSecureTextEntry:YES];
[usernameLT setTextFieldDelegate:self];
usernameLT.tag = 100;
LTView.h
@interface LTView : UIView
{
id<UITextFieldDelegate> _textFieldDelegate;
}
-(id)initWithFrame:(CGRect)frame descriptionText:(NSString *)text;
//设定自定义视图中的 密文输入是否实现
-(void)setSecureTextEntry:(BOOL)isSecure;
//从类的外部传人的UITextFieldDelegate的代理对象
@property(nonatomic ,assign)id<UITextFieldDelegate>textFieldDelegate;
//回收键盘
-(void)resignKeyBord;
@end
LTView.m
@interface LTView ()
{
UILabel * _descriptionLabel;//显示描述信息
UITextField * _inputTextField;//输入框
}
@end
@implementation LTView
//重写了父类的初始化方法
- (id)initWithFrame:(CGRect)frame
{
self = [self initWithFrame:frame descriptionText:nil];
return self;
}
-(id)initWithFrame:(CGRect)frame descriptionText:(NSString *)text
{
self = [super initWithFrame:frame];
if (self) {
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
//添加子视图Label
_descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width/3, height)];
_descriptionLabel.text = text;
[self addSubview:_descriptionLabel];
_descriptionLabel.backgroundColor = [UIColor yellowColor];
[_descriptionLabel release];
//添加子视图TextField
_inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(width/3, 0, width-width/3, height)];
_inputTextField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:_inputTextField];
[_inputTextField release];
}
return self;
}
//设定自定义视图中的 密文输入是否实现
-(void)setSecureTextEntry:(BOOL)isSecure
{
_inputTextField.secureTextEntry = isSecure;
}
//回收键盘
-(void)resignKeyBord
{
NSLog(@"fsdgdfsg");
[_inputTextField resignFirstResponder];
}
-(void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate
{
_inputTextField.delegate = textFieldDelegate;
}