通过继承制作简单的控件类


      苹果给开发者提供了UITextField,UILabel,UIButton等简单的控件。但是仅仅这些控件远远不能满足我们成员的需求,所以需要我们自己制作控件,来实现我们的需求。曾经有人这么说过,在iphone里你看到的,摸到的,都是UIView,所以UIView在iphone开发里具有非常重要的作用。我们通过继承UIView来实现我们自己的控件(代码如下.h文件):

@interface MyCombinationControl : UIView  <UITextFieldDelegate>
{
    @private
    UITextField* _textFeild; //定义一个编辑框
    UILabel* _label;//定义一个
}

@property(nonatomic,retain)NSString* title; //设置标题
@property(nonatomic,retain)NSString* placeHolderStr;//设置占位符
@property(nonatomic,retain)NSString* text;//文本内容
@property(nonatomic,assign)BOOL secureTextEntry;//是否是密码框
-(id)initWithFrame:(CGRect)frame title:(NSString*)title placeHolderStr:(NSString*)placeHolderStr secureTextEntry:(BOOL)secureTextEntry;//初始化方法
@end


以下是实现:

@implementation MyCombinationControl


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        

    }
    return self;
}

-(id)initWithFrame:(CGRect)frame title:(NSString *)title placeHolderStr:(NSString *)placeHolderStr secureTextEntry:(BOOL)secureTextEntry{
    self  = [self initWithFrame:frame];
    if (self) {
        //定义基本的label控件
        _label= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/3, frame.size.height)];//通过比例分配控件大小及其位置
        _label.backgroundColor=[UIColor redColor];//设置label的背景颜色
        _label.text = title;//设置label标题
        _label.font = [UIFont boldSystemFontOfSize:20];//设置label字体
        _label.textColor=[UIColor blueColor];//设置label的文本的字体颜色
        _label.baselineAdjustment=UIBaselineAdjustmentAlignCenters;//控制文本基线时调整文本需要收缩标签
        _label.textAlignment = NSTextAlignmentCenter;//排列方式
        //定义基本的textfield控件
        _textFeild = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];//
        _textFeild.layer.cornerRadius =15;//圆角半径
        _textFeild.delegate=self;//设置代理
        _textFeild.placeholder=placeHolderStr;//占位符
        _textFeild.textColor = [UIColor  redColor];//设置文本颜色
        _textFeild.clearsOnInsertion = YES;//插入光标删除
        _textFeild.clearButtonMode = YES;//是否显示清除按钮
        _textFeild.borderStyle = UITextBorderStyleRoundedRect;//边框样式
        _textFeild.autocorrectionType = UITextAutocorrectionTypeNo;//取消自动提示
        _textFeild.autocapitalizationType=UITextAutocapitalizationTypeNone;//首字母大小写
        _textFeild.spellCheckingType=UITextSpellCheckingTypeNo;//拼写检查
        _textFeild.keyboardAppearance=UIKeyboardAppearanceAlert;//键盘样式
        _textFeild.returnKeyType=UIReturnKeyGo;//return样式
        _textFeild.enablesReturnKeyAutomatically =YES;//该输入框的内容是否必须
        _textFeild.secureTextEntry = secureTextEntry;//文本内容是否是密码
        _textFeild.layer.borderColor = [[UIColor redColor]CGColor];//边框颜色
        _textFeild.layer.borderWidth=3.0f;//边框大小
        _textFeild.leftView = _label;
        _textFeild.leftViewMode=UITextFieldViewModeAlways;
        _textFeild.layer.opacity=0.5;
        [self addSubview:_textFeild];
    }
    return self;
}
-(NSString*)text{
    return  _textFeild.text;
}
-(void)setText:(NSString *)text{
    _textFeild.text  = text;
}
-(BOOL)secureTextEntry{
    return _textFeild.secureTextEntry;
}
-(void)setSecureTextEntry:(BOOL)secureTextEntry{
    _textFeild.secureTextEntry = secureTextEntry;
}

@end

以上实现一个基本的控件效果图如下:


今天我们先讲通过继承实现简单控件封装,下次实现通过类目,延展。以及他们的组合体实现稍微复杂的封装方式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值