自定义视图:视图的封装过程.

来源:互联网 发布:剑灵人男捏脸数据图 编辑:程序博客网 时间:2024/06/05 04:00
@interface LTView : UIView//1.是否采用安全模式- (void)setSecureEntry:(BOOL)secureEntry;//2.设置键盘的类型- (void)setKeyBoardType:(UIKeyboardType)keyBoardType;//3.设置textField代理- (void)setDelegate:(id<UITextFieldDelegate>)delegate;//4.获取输入框中输入的文字- (NSString *)text;//自定义初始化方法,来处理LTView之间的差异性//1.LTView的frame, 2.UILabel上显示的文字 . 3.UITextField占位文字. 4.默认显示的文字.- (id)initWithFrame:(CGRect)frame labelText:(NSString *)labelText placeholder:(NSString *)placeholder textFieldText:(NSString *)textFieldText;@end

LTView.h中的接口文件.

@interface LTView (){    UILabel *_desLabel; //左边的label    UITextField *_textField; //右边的textField}@end@implementation LTView- (id)initWithFrame:(CGRect)frame labelText:(NSString *)labelText placeholder:(NSString *)placeholder textFieldText:(NSString *)textFieldText{    self = [self initWithFrame:frame];    if (self) {        //initialization code here..        _desLabel.text = labelText;        _textField.placeholder = placeholder;        _textField.text = textFieldText;    }    return self;}/** *  当系统提供的控件满足不了我们的需求时,我们就需要组合系统控件,封装成自己的控件.    自定义视图的步骤:    1.创建类 继承自UIView.    2.封装系统控件.(内部完成组合控件的创建过程)    3.使用自定义控件创建对象.    封装的过程:封装内部实现,提供外界访问的接口.先封装公共部分,然后处理差异. * */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        //LTView内部封装了 左边一个UILabel,右边一个UITextField.        // Initialization code        //初始化时完成内部封装控件的创建以及添加操作.        CGFloat width = frame.size.width;        CGFloat height = frame.size.height;        //UILabel        _desLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0.3 * width, height)];        _desLabel.font = [UIFont systemFontOfSize:15];        _desLabel.textAlignment = NSTextAlignmentRight;        [self addSubview:_desLabel];        [_desLabel release];        //UITextField        _textField = [[UITextField alloc] initWithFrame:CGRectMake(0.4 * width, 0, 0.6 * width, height)];        _textField.borderStyle = UITextBorderStyleRoundedRect;        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];        [btn setTitle:@"完成" forState:UIControlStateNormal];        [btn addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];        btn.frame = CGRectMake(320 - 60, 0, 40, 40);        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];        [view addSubview:btn];        view.backgroundColor = [UIColor whiteColor];        _textField.inputAccessoryView = view;        [view release];        _textField.autocorrectionType = UITextAutocorrectionTypeNo;        [self addSubview:_textField];        [_textField release];    }    return self;}- (void)done:(UIButton *)btn{    [_textField resignFirstResponder];}//1.是否采用安全模式- (void)setSecureEntry:(BOOL)secureEntry{    _textField.secureTextEntry = secureEntry;}//2.设置键盘的类型- (void)setKeyBoardType:(UIKeyboardType)keyBoardType{    _textField.keyboardType = keyBoardType;}//3.设置textField代理- (void)setDelegate:(id<UITextFieldDelegate>)delegate{    _textField.delegate = delegate;}//4.获取输入框中输入的文字- (NSString *)text{    return _textField.text;}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end

LTView.m中中的实现.

通过以上两步,可以封装左边一个UILable,和右边一个UITextField视图.

0 0