UI_自定义视图_code

Day 03 of UI

LTView(自定义视图)

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 设置window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.window release];

// 创建一个LTView对象,添加到Window上
LTView *useNameLTView = [[LTView alloc] initWithFrame:CGRectMake(50, 50, CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 35)];
useNameLTView.backgroundColor = [UIColor cyanColor];
[self.window addSubview:useNameLTView];
useNameLTView.title = @"用户名";
useNameLTView.placeHolder = @"请输入用户名";
[useNameLTView release];

LTView *passwordLTView = [[LTView alloc] initWithFrame:CGRectMake(50, 150, CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 35) title:@"密码" placeHolder:@"请输入密码"];
passwordLTView.backgroundColor = [UIColor cyanColor];
passwordLTView.isSecureText = 1;
passwordLTView.delegate = self;
[self.window addSubview:passwordLTView];
[passwordLTView release];

return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"22");
[textField resignFirstResponder];
return YES;
}


LTView.h

@interface LTView : UIView

#pragma mark - 声明属性

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *placeHolder;

@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) id <UITextFieldDelegate>delegate;
@property (nonatomic, assign) BOOL isSecureText;

#pragma mark - 声明自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame
                        title:(NSString *)title
                  placeHolder:(NSString *)placeHolder;

@end


LTView.m

@interface LTView()
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textfield;

@end

@implementation LTView


@synthesize text = _text;
#pragma mark - 重写dealloc方法
- (void)dealloc
{
[_label release];
[_textfield release];

[super dealloc];
}

#pragma mark - 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 1. 创建左侧的Label
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame) / 3, CGRectGetHeight(frame))] autorelease];

self.label.backgroundColor = [UIColor redColor];
[self addSubview:_label];

// 2. 创建右侧的TextField
self.textfield = [[[UITextField alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame) / 3, 0, CGRectGetWidth(frame) * 2 / 3, CGRectGetHeight(frame))] autorelease];

[self addSubview:_textfield];

}
return self;
}

#pragma mark - 重写title的set方法,把外界的值传过来
- (void)setTitle:(NSString *)title
{
if (_title != title) {
[_title release];
_title = [title copy];

_label.text = title;
}
}


- (void)setPlaceHolder:(NSString *)placeHolder
{
if (_placeHolder != placeHolder) {
[_placeHolder release];
_placeHolder = [placeHolder copy];

_textfield.placeholder = _placeHolder;
}
}

#pragma mark - 实现自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame
title:(NSString *)title
placeHolder:(NSString *)placeHolder
{
self = [self initWithFrame:frame];
if (self) {
_label.text = title;
_textfield.placeholder = placeHolder;
}
return self;
}

-(void)setDelegate:(id<UITextFieldDelegate>)delegate
{
_textfield.delegate = delegate;
}

- (void)setText:(NSString *)text
{
if (_text != text) {
[_text release];
_text = [text copy];
_textfield.text = text;
}
}

- (NSString *)text
{
return _textfield.text;
}

- (void)setIsSecureText:(BOOL)isSecureText
{
_textfield.secureTextEntry = isSecureText;
}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值