自定义视图

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值