UI自定义视图

将label和textField写成一个view

新建一个UIView子类

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface LTView : UIView
 4 {
 5     //  写在实例变量里面外界不能访问
 6     UILabel *_label;           //  标签
 7     UITextField *_textField;   //  输入框
 8 }
 9 //  外界可访问
10 @property (nonatomic, copy) NSString *labelText;  //  标签的文字
11 @property (nonatomic, retain) UIColor *lableTextColor;
12 @property (nonatomic, copy) NSString *textFieldText;
13 @property (nonatomic, assign, getter = isSecureTextEntry) BOOL secureTextEntry;  //  设置是否以圆点方式显示
14 @property (nonatomic, assign) id<UITextFieldDelegate> delegate;
15 
16 #pragma mark 初始化方法
17 - (instancetype)initWithFrame:(CGRect)frame withLabelText:(NSString *)text;
18 
19 @end

实现部分

 1 #import "LTView.h"
 2 
 3 @implementation LTView
 4 
 5 - (void)dealloc
 6 {
 7     [_label release];
 8     [_textField release];
 9     
10     [super dealloc];
11 }
12 
13 #pragma mark 重写initWithFrame:
14 - (id)initWithFrame:(CGRect)frame
15 {
16     self = [super initWithFrame:frame];
17     if (self) {
18 //        self.backgroundColor = [UIColor cyanColor];
19         
20         //  添加Label
21         _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)];
22 //        _label.backgroundColor = [UIColor grayColor];
23         [self addSubview:_label];
24         
25         //  添加TextField
26         _textField = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width / 3, 0, frame.size.width / 3 * 2, frame.size.height)];
27         _textField.borderStyle = UITextBorderStyleRoundedRect;
28         [self addSubview:_textField];
29     }
30     return self;
31 }
32 
33 #pragma mark 重写setter和getter方法
34 - (void)setLabelText:(NSString *)labelText
35 {
36     _label.text = labelText;
37 }
38 - (NSString *)labelText
39 {
40     return _label.text;
41 }
42 
43 - (void)setLableTextColor:(UIColor *)lableTextColor
44 {
45     _label.textColor = lableTextColor;
46 }
47 
48 - (void)setTextFieldText:(NSString *)textFieldText
49 {
50     _textField.text = textFieldText;
51 }
52 - (NSString *)textFieldText
53 {
54     return _textField.text;
55 }
56 
57 - (void)setSecureTextEntry:(BOOL)secureTextEntry
58 {
59     _textField.secureTextEntry = secureTextEntry;
60 }
61 - (BOOL)isSecureTextEntry
62 {
63     return _textField.secureTextEntry;
64 }
65 
66 - (void)setDelegate:(id<UITextFieldDelegate>)delegate
67 {
68     _textField.delegate = delegate;
69 }
70 
71 #pragma mark 实现初始化方法
72 - (instancetype)initWithFrame:(CGRect)frame withLabelText:(NSString *)text
73 {
74     self = [self initWithFrame:frame];
75     if (self) {
76         _label.text = text;
77     }
78     return self;
79 }
80 
81 @end

AppDelegate.m中

 1 LTView *view = [[LTView alloc] initWithFrame:CGRectMake(30, 50, 200, 30) withLabelText:@"用户名"];
 2     view.delegate = self;
 3 //    view.SecureTextEntry = YES;
 4     [self.window addSubview:view];
 5     [view release];
 6     
 7     LTView *view2 = [[LTView alloc] initWithFrame:CGRectMake(30, 100, 200, 30) withLabelText:@"密码"];
 8     view2.delegate = self;
 9     view2.SecureTextEntry = YES;
10     [self.window addSubview:view2];
11     [view2 release];

 

posted @ 2014-05-14 12:08  九三零  阅读(187)  评论(0编辑  收藏  举报