如何封装UILable 输入框 和UIField 显示框 同时创建对象(经典)

源代码(.m文件)


#import "TLView.h"

//本类的延展

@interface TLView ()

{

    UILabel *_desLabel;    //左边的lable

    UITextField *_textField;//右边的lable

}

@end


@implementation TLView

//改写父类的初始化方法,处理相同的性能

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // 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:18];

        _desLabel.textAlignment = NSTextAlignmentRight;

        //        _desLabel.backgroundColor = [UIColor lightGrayColor];

        [self addSubview:_desLabel];

        [_desLabel release];

        

        //UITextfield

        _textField = [[UITextField alloc] initWithFrame:CGRectMake(0.4 * width, 0 , 0.6 * width , height)];

        _textField.borderStyle = UITextBorderStyleRoundedRect;

        //        _textField.backgroundColor = [UIColor lightGrayColor];

        _textField.autocorrectionType = UITextAutocorrectionTypeNo;

        [self addSubview:_textField];

        [_textField release];


    }

    return self;

}

//改写初始化方法,处理不同的部分

- (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,是否采用安全模式

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

}

@end


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值