通过继承制作简单的控件类
来源:互联网 发布:网站 ip地址 域名绑定 编辑:程序博客网 时间:2024/05/22 05:14
苹果给开发者提供了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
以上实现一个基本的控件效果图如下:
今天我们先讲通过继承实现简单控件封装,下次实现通过类目,延展。以及他们的组合体实现稍微复杂的封装方式
- 通过继承制作简单的控件类
- 简单日期控件的制作
- 制作简单的ascx用户控件
- Extjs 通过继承自定义控件
- 探究Android中通过继承ViewGroup自定义控件的原理
- 探究Android中通过继承ViewGroup自定义控件的原理
- 继承控件类的弊端
- 通过继承消除重复代码的简单示例
- JS通过prototype实现继承的简单示例:
- 8 面向对象(制作帮助文档,通过JDK提供的API学习了Math类,代码块,继承(继承子父成员访问特点,super关键字,方法重写))
- 通过jsonp跨域问题制作简单的天气
- CLR自定义.NET控件制作(1)——制作简单的自定义控件
- 制作OCX控件简单流程
- ActiveX控件从制作到引用的简单过程
- 一个简单的帮助文档制作工具 Treeview控件
- ActiveX控件从制作到引用的简单过程
- 利用Blend制作一个简单的用户控件外观
- asp.net 用Repeater控件制作的简单…
- ENGLISH资料收集(44)-少管闲事
- linear regression 线性回归
- 程序人生的四个象限和两条主线
- H题
- linux的poll机制
- 通过继承制作简单的控件类
- go语言的面向对象
- 程序人生的四个象限和两条主线
- 卫星影像的分辨率与成图比例尺
- 从终端与云端的变化看未来30年(孙正义)
- EditText点击两次才能响应Onclick
- ORACLE存储过程笔记2----运算符和表达式
- sql的增删改查
- fastboot 选型、编译