首页 > 代码库 > 如何封装UILable 输入框 和UIField 显示框 同时创建对象(经典)
如何封装UILable 输入框 和UIField 显示框 同时创建对象(经典)
源代码(.m文件)
#import "TLView.h"
//本类的延展
@interface TLView ()
{
UILabel *_desLabel; //左边的lable
UITextField *_textField;//右边的lable
}
@end
@implementation TLView
//改写父类的初始化方法,处理相同的性能
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
//UIlabel
_desLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 0.3 * width, height)];
_desLabel.font = [UIFontsystemFontOfSize:18];
_desLabel.textAlignment =NSTextAlignmentRight;
// _desLabel.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_desLabel];
[_desLabel release];
//UITextfield
_textField = [[UITextFieldalloc] 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 = [selfinitWithFrame: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