ios中输入框的父类--文本框,DataPick,pickerview
父控制器
#import <UIKit/UIKit.h>
#import "ScrollViewExt.h"
@interface BaseKeyBoardCtrl : UIViewController<UITextFieldDelegate>
@property(nonatomic,assign)ScrollViewExt *scrollviewExt;
//键盘隐藏
-(void)keyboardHide;
//选择器
-(void)selectPickerType:(int)type data:(NSArray *)data tag:(int)tag;
-(void)startHideView:(NSString *)str;
//设置scrollview的大小
-(void)SetScrollviewHeight:(CGFloat)contentHeight;
@end
//
// BaseKeyBoardCtrl.m
// ProgramDemo
//
// Created by zy on 13-11-14.
// Copyright (c) 2013年 zy. All rights reserved.
//
#import "BaseKeyBoardCtrl.h"
#import "PresentView.h"
#import "JSPresentCommonViewCtrl.h"
#import "DIYTextField.h"
@interfaceBaseKeyBoardCtrl (){
int _pickerTag;//选择器的tag
CGFloat _keyboardHeight;//键盘高度
int _diyTextFieldTag;//DiyTextFile的tag
UITextField *_nextFiled;//下一个控件 -键盘事件
BOOL flag;//键盘标志
}
@end
@implementation BaseKeyBoardCtrl
#pragma mark -生命周期方法
- (void)dealloc
{
[[NSNotificationCenterdefaultCenter] removeObserver:self];
[super dealloc];
}
- (void)viewDidLoad
{
[superviewDidLoad];
flag=NO;
if (IOS7) self.edgesForExtendedLayout=UIRectEdgeNone;//对导航栏和状态栏同时存在有效
[selfsetupTopNavigationView];
self.view.backgroundColor=[UIColorwhiteColor];
//注册键盘事件
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
CGFloat y=(IOS7==YES)?64:44;
self.scrollviewExt=[[ScrollViewExtalloc] initWithFrame:CGRectMake(0, y, self.view.bounds.size.width, 10000)];
_scrollviewExt.delegate=self;
_scrollviewExt.contentSize=self.view.bounds.size;
_scrollviewExt.contentOffset=CGPointMake(0, 0);
[self.viewaddSubview:_scrollviewExt];
[_scrollviewExtrelease];
}
#pragma mark-设置scrollview的大小
-(void)SetScrollviewHeight:(CGFloat)contentHeight{
CGFloat y=(IOS7==YES)?64:44;
self.scrollviewExt.frame=CGRectMake(0, y, self.view.bounds.size.width, self.view.bounds.size.height);
self.scrollviewExt.contentSize=CGSizeMake(self.view.bounds.size.width, contentHeight);
}
#pragma mark -ScrollViewExt Delegate
-(void)keyboardHide{
[_scrollviewExtendEditing:YES];
}
#pragma mark -键盘弹出
-(void)keyboardWasShown:(NSNotification*)aNotification{
NSDictionary *info=[aNotification userInfo];
CGRect KeyBoardrect=[info[UIKeyboardBoundsUserInfoKey] CGRectValue];
int curve=[info[UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat duration=[info[UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIWindow *keyWindow = [[UIApplicationsharedApplication] keyWindow];
//找第一响应者
UIView *firstResponderView = [keyWindow performSelector:@selector(firstResponder)];
if ([firstResponderView isKindOfClass:[UITextField class]] ) {
//view的高度
CGFloat Viewheight=self.view.bounds.size.height;
//第一响应者转换坐标
CGRect FirstRect=[firstResponderView convertRect:firstResponderView.bounds toView:self.scrollviewExt];
CGFloat y=FirstRect.origin.y+FirstRect.size.height;//文本框的高度
//键盘的高度
CGFloat keyboardheight=KeyBoardrect.size.height+44;//键盘高度--》中文高度44
_keyboardHeight=keyboardheight;
CGFloat h=Viewheight-y-keyboardheight;
if(h<10){
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationCurve:curve];
[UIViewsetAnimationDuration:duration];
_scrollviewExt.contentOffset=CGPointMake(0, -h+44);
[UIViewcommitAnimations];
}
}
}
#pragma mark -隐藏键盘
-(void) keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary *info=[aNotification userInfo];
int curve=[info[UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat duration=[info[UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationCurve:curve];
[UIViewsetAnimationDuration:duration];
if (flag) {
flag=NO;
}
else{
_scrollviewExt.contentOffset=CGPointMake(0, 0);
}
[UIViewcommitAnimations];
}
#pragma mark 递归找出第一响应者
- (UITextField *)findFistResponder:(UIView *)view {
for (UIView *child in view.subviews) {
if ([child respondsToSelector:@selector(isFirstResponder)]
&&
[child isFirstResponder]) {
return (UITextField *)child;
}
UITextField *field = [self findFistResponder:child];
if (field) {
return field;
}
}
returnnil;
}
#pragma mark-UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
UIReturnKeyType returnType=textField.returnKeyType;
if (returnType==UIReturnKeyDone) [selfkeyboardHide];
else{
UITextField *NextTxt=(UITextField *)[self.scrollviewExtviewWithTag:textField.tag+1];
[NextTxt becomeFirstResponder];
[UIViewanimateWithDuration:0.25 animations:^{
[selfscrollviewWithNextTextField:NextTxt];
}];
}
returnYES;
}
#pragma mark -UITextField和DIYTextField公用方法
//键盘弹出,UIScrollerView滚动
-(void)scrollviewWithNextTextField:(UITextField *)txt{
//view的高度
CGFloat Viewheight=self.view.bounds.size.height;
//第一响应者转换坐标
CGRect FirstRect=[txt convertRect:txt.boundstoView:self.scrollviewExt];
CGFloat y=FirstRect.origin.y+FirstRect.size.height;//文本框的高度
CGFloat h=Viewheight-y-_keyboardHeight;
if(h<10) _scrollviewExt.contentOffset=CGPointMake(0, -h+40);
}
#pragma mark -DIYTextField Delegate
-(void)DIYFieldCustomButtonAction:(DIYTextField *)txt{
UITextField *NextTxt=(UITextField *)[self.scrollviewExtviewWithTag:_diyTextFieldTag+1];
[NextTxt becomeFirstResponder];
[UIViewanimateWithDuration:0.25 animations:^{
[selfscrollviewWithNextTextField:NextTxt];
}];
}
-(void)keyboardShow:(DIYTextField *)textField{
_diyTextFieldTag=textField.tag;
[selfscrollviewWithNextTextField:textField];
[textField addCustomButton:@"NumberPad-Empty" title:@"下一项" target:self action:@selector(DIYFieldCustomButtonAction:)];
}
-(void)keyboardHide:(DIYTextField *)textField{
[textField delCustomButton:@"NumberPad-Empty"];
}
#pragma mark -选择器事件
/*
type =0:是代表数据源 data必须有值
=1是代表显示时间 data=nil
tag 是控件表示必须有
*/
-(void)selectPickerType:(int)type data:(NSArray *)data tag:(int)tag{
flag=YES;
[selfkeyboardHide];
UITextField *txt =(UITextField *) [_scrollviewExtviewWithTag:tag+1];
if ([txt isKindOfClass:[UITextFieldclass]]) {
_nextFiled=txt;
}
for (UIViewController *vc inself.childViewControllers) {
[vc removeFromParentViewController];
}
_pickerTag=tag;
JSPresentCommonViewCtrl *ctrl=[[JSPresentCommonViewCtrlalloc] initWithPresentType:type WithDataArray:data];
ctrl.delegate=self;
ctrl.method=@selector(startHideView:);
CGRect hrect,rect=self.view.bounds;
CGRectDivide(rect, &hrect, &rect, 180, CGRectMaxYEdge);
[PresentViewshowWithSubView:ctrl.viewsubVFrame:hrect];
if (iPhone5) {
ctrl.view.frame = CGRectMake(0, 582-260, 320, 260);
}else{
ctrl.view.frame = CGRectMake(0, 220, 320, 260);
}
[selfaddChildViewController:ctrl];
[ctrl release];
}
#pragma mark-隐藏选择器
-(void)startHideView:(NSString *)str{
[PresentViewhidePresentSubView];
[_nextFiledbecomeFirstResponder];
if (str.length>0) {
UIButton *btn=(UIButton *)[self.viewviewWithTag:_pickerTag];
[btn setTitle:str forState:UIControlStateNormal];
}
}
@end
封装控件view
#import <UIKit/UIKit.h>
#import "DIYTextField.h" @interface InputView : UIView #pragma mark- 集合批量生成对应的控件--展示
-(id)initWithShowFrame:(CGRect)frame valueDic:(NSDictionary *)dic rowHeight:(CGFloat)height SplitWidth:(CGFloat)width; #pragma mark- 集合批量生成对应的控件--编辑插入
-(id)initWithEditFrame:(CGRect)frame TextArray:(NSArray *)textArr flagAray:(NSArray *)flagArr ValueArray:(NSArray *)valueArr rowHeight:(CGFloat)height SplitWidth:(CGFloat)width tag:(int)tag delegate:(id)delegate isplaceHolder:(BOOL)isShow method:(SEL)method; @end =================
#import "InputView.h"
#import "FyGroupLineView.h"
@implementation InputView #pragma mark -展示控件-多个控件
/*
valueDic:包含key-value.
rowHeight ->行高
width 等于0 自动排版,>0指定排版
*/
-(id)initWithShowFrame:(CGRect)frame valueDic:(NSDictionary *)dic rowHeight:(CGFloat)height SplitWidth:(CGFloat)width {
self = [super initWithFrame:frame];
if (self) {
CGRect hrect,vrect,rect=UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(, , , )); FyGroupLineView *lineView=[[FyGroupLineView alloc] initWithFrame:rect WithLineNumbers:dic.allKeys.count];
[self addSubview:lineView];
[lineView release];
for (NSString *key in dic.allKeys) {
NSString *value=dic[key];
CGRectDivide(rect, &hrect,&rect , height, CGRectMinYEdge);
if (width==) {
width=[key sizeWithFont:[UIFont systemFontOfSize:] constrainedToSize:CGSizeMake(self.bounds.size.width, ) lineBreakMode:NSLineBreakByWordWrapping].width+;
}
CGRectDivide(hrect, &vrect, &hrect, width, CGRectMinXEdge);
UILabel *lb=[UILabel LabWithFrame:UIEdgeInsetsInsetRect(vrect, UIEdgeInsetsMake(, , , )) text:[NSString stringWithFormat:@"%@",key] textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:]];
[self addSubview:lb];
lb=[UILabel LabWithFrame:UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(, , , )) text:value textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:]];
[self addSubview:lb];
}
}
return self;
} #pragma mark -编辑和插入控件-多个控件
/*
同时生成多个控件---
textArr-->左边名称集合
flagArr--->对应控件的标志集合
0--》文本框 键盘返回类型 英文和数字,
1--》文本框 键盘返回类型 电话号码
2--》/文本框 键盘返回类型 电子邮件
3--》button 选择器
valueArr。如果有值且集合大小和textArr相同,就是编辑状态 否则是插入状态
rowHeight 每一行高度
SplitWidth 每一行分割高度
tag ->控件对应的标志
isplaceHolder是否展示isplaceHolder
method,只针对按钮控件,且是按钮控件的触发事件
*/ -(id)initWithEditFrame:(CGRect)frame TextArray:(NSArray *)textArr flagAray:(NSArray *)flagArr ValueArray:(NSArray *)valueArr rowHeight:(CGFloat)height SplitWidth:(CGFloat)width tag:(int)tag delegate:(id)delegate isplaceHolder:(BOOL)isShow method:(SEL)method{
self = [super initWithFrame:frame];
if (self) {
CGRect hrect,vrect,rect=UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(, , , )); FyGroupLineView *lineView=[[FyGroupLineView alloc] initWithFrame:rect WithLineNumbers:textArr.count];
[self addSubview:lineView];
[lineView release]; for (int i=; i<textArr.count; i++) {
CGRectDivide(rect, &hrect, &rect,height, CGRectMinYEdge);
CGRectDivide(hrect, &vrect, &hrect, width, CGRectMinXEdge); UILabel *lb=[UILabel LabWithFrame:UIEdgeInsetsInsetRect(vrect, UIEdgeInsetsMake(, , , )) text:[NSString stringWithFormat:@"%@:",textArr[i]] textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:]];
[self addSubview:lb]; int flag=[flagArr[i] intValue];//对应控件的标志 if (flag!=) {
UIImageView *imgview=[UIImageView ImageViewImageName:@"txField_image.png" frame:UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(, , , ))];
[self addSubview:imgview];
} NSString *placeHolder=(isShow==YES)?[NSString stringWithFormat:@"请输入%@",textArr[i]]:@"";
UITextField *txt=nil;
CGRect rightRect=UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(, , , ));
if (flag ==){ //文本框 键盘返回类型 UIKeyboardTypeNamePhonePad,
if (i==textArr.count-)
txt=[UITextField TextFieldWithFrame:rightRect target:delegate textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:placeHolder clearMode:UITextFieldViewModeWhileEditing returnKey:UIReturnKeyDone keyBord:UIKeyboardTypeNamePhonePad];
else
txt=[UITextField TextFieldWithFrame:rightRect target:delegate textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:placeHolder clearMode:UITextFieldViewModeWhileEditing returnKey:UIReturnKeyNext keyBord:UIKeyboardTypeNamePhonePad];
}
else if (flag==){//文本框 键盘返回类型 UIKeyboardTypePhonePad,
if (i==textArr.count-)
txt=[DIYTextField DIYTextFieldWithFrame:rightRect target:nil diyTarget:delegate txColor:[UIColor blackColor] placeHolder:placeHolder];
else
txt=[DIYTextField DIYTextFieldWithFrame:rightRect target:nil diyTarget:delegate txColor:[UIColor blackColor] placeHolder:placeHolder];
}
else if (flag==){//文本框 键盘返回类型 电子邮件
if (i==textArr.count-)
txt=[UITextField TextFieldWithFrame:rightRect target:delegate textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:placeHolder clearMode:UITextFieldViewModeWhileEditing returnKey:UIReturnKeyDone keyBord:UIKeyboardTypeEmailAddress];
else
txt=[UITextField TextFieldWithFrame:rightRect target:delegate textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft placeHolder:placeHolder clearMode:UITextFieldViewModeWhileEditing returnKey:UIReturnKeyNext keyBord:UIKeyboardTypeEmailAddress];
}
else if (flag==)//按钮 时间控件DatePicker
txt=(UITextField *)[UIButton ButtonWithImageName:@"txField_image.png" hImageName:@"txField_image.png" frame:UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(, ,, )) title:[NSString stringWithFormat:@"请选择%@",textArr[i]] titleColor:[UIColor blackColor] font:[UIFont systemFontOfSize:] target:delegate action:method]; txt.tag=tag+i;
if (i==textArr.count-) {
if ([txt isKindOfClass:[UITextField class]]) txt.returnKeyType=UIReturnKeyDone;
}
if (valueArr.count==textArr.count) {
NSString *value=valueArr[i];
if ([txt isKindOfClass:[UIButton class]]) {
UIButton *btn=(UIButton *)txt;
[btn setTitle:value forState:UIControlStateNormal];
}
else txt.text=value; }
[self addSubview:txt]; }
}
return self;
}
画线
#import <UIKit/UIKit.h> @interface FyGroupLineView : UIView - (id)initWithFrame:(CGRect)frame WithLineNumbers:(int)cols; @end #import "FyGroupLineView.h"
#import <QuartzCore/QuartzCore.h> @implementation FyGroupLineView - (id)initWithFrame:(CGRect)frame WithLineNumbers:(int)cols
{
self = [super initWithFrame:frame];
if (self) { UIImageView *background = [[UIImageView alloc] initWithFrame:self.bounds];
background.image = [UIImage ImageWithColor:[UIColor whiteColor] frame:self.bounds];
background.layer.borderWidth = 0.5;
background.layer.borderColor = rgb(, , ).CGColor;
background.layer.cornerRadius = ;
background.layer.masksToBounds = YES;
[self addSubview:background];
[background release]; CGRect vRect = self.bounds,lineRect;
for (int i = ; i<cols-; i++) {
CGRectDivide(vRect, &lineRect, &vRect, self.bounds.size.height/cols, CGRectMinYEdge); lineRect = UIEdgeInsetsInsetRect(lineRect, UIEdgeInsetsMake(lineRect.size.height-0.5, , -0.5, ));
UILabel *lineLab = [UILabel LabWithFrame:lineRect text:nil textColor:nil textAlign:NSTextAlignmentCenter font:[UIFont systemFontOfSize:]];
lineLab.backgroundColor = rgb(, , );
[self addSubview:lineLab];
} }
return self;
}
ios中输入框的父类--文本框,DataPick,pickerview的更多相关文章
- IOS中UITextView(多行文本框)控件的简单用法
1.创建并初始化 UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.UITextField的用处多,UITextVie ...
- 在C# 中 如何限制在文本框(textBox)中输入的类型为正整数
在文本框的 KeyPress 事件中写下这些代码就可以保证是正整数了 private void textBox1_KeyPress(object sender, KeyPressEventArgs e ...
- html中radio单选和文本框限制只能输入数字的解决方案
一.当html中存在多个radio单选按钮时将所有的单选按钮name属性设置为一样,就可实现每次只选中一个的效果. 二.限制文本框只能输入数字,代码如下: $(function(){ $(" ...
- wpf中通过ObjectDataProvider实现文本框的双向数据绑定(ps:适用于在文本框比较多的时候使用)
前端代码: 也页面的xaml中引入ObjectDataProvider: <Window.Resources> <ResourceDictionary> <ObjectD ...
- MFC中如何给静态文本框添加消息响应
需要两个步骤: 第一个: 是改变它的ID(默认情况下所有的静态文本框的ID都为IDC_STATIC,你需要改变他的ID为其他的值). 第二个: 是在它的属性对话框中选中Notify选项,VS是将该属性 ...
- C#判断页面中的多个文本框输入值是否有重复的实现方法
List<string> list = new List<string>();//首先定义一个泛型数组 //这里假如说有四个文本框 string mainseat = this ...
- ios中的三种弹框《转》
目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = ...
- ios中的三种弹框
目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = ...
- IOS中position:fixed弹出框中的input出现光标错位的问题
解决方案是 在弹框出现的时候给body添加fixed <style type="text/css"> body{ position: fixed; width: 100 ...
随机推荐
- WebApi的调用-3.Basic验证
webapi里的特性 /// <summary> /// Basic验证 /// </summary> /// <remarks> /// /// </rem ...
- (APIO2014)序列分割
题解: 我也不知道为啥上午上课讲了我昨天看的3题 这题关键在于发现操作顺序无关的 可以发现最终答案是任意两段乘积的和 那这个东西显然是可以dp的 然后可以斜率优化一波 nklongn 另外上课讲的是当 ...
- Python 模块介绍
一.模块:用一坨代码实现了某个功能的代码集合. 二.模块分为三种 1.自定义模块 2.内置标准模块(又称标准库) 3.开源模块(上传方式,百度PyPi) 开源模块安装方式: a.yum b.pip c ...
- P1219 八皇后 含优化 1/5
题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...
- Redis数据结构之set
一:介绍 1.set结构 没有顺序 并且,不允许出现重复的元素. 二:Redis客户端的常用命令 1.添加 2.查看数据 3.删除数据 4.是否存在某个值 1代表有,0代表无. 5.查看差值 有key ...
- 037 关于pom.xml的一些问题的理解
最近在pom上出了一些问题,搞了一天才理解了一些问题,记录一下. 当在覆盖本地repository包之后,pom.xml上面出现了一个x. 当mvn->update project之后,还是有许 ...
- [OpenCV-Python] OpenCV 中视频分析 部分 VI
部分 VI视频分析 OpenCV-Python 中文教程(搬运)目录 39 Meanshift 和 和 Camshift 目标 • 本节我们要学习使用 Meanshift 和 Camshift 算法在 ...
- 在docker中运行mysql实例
Docker是一种新兴的虚拟化技术,能够一定程度上的代替传统虚拟机.下图是容器跟虚拟机的对比 对docker有个大致了解,学习docker断断续续,虽说学习不能急于求成,但断断续续学的话,浪费的碎片化 ...
- LoRaWAN 1.1 网络协议规范 - 3 物理层帧格式
LoRaWAN 1.1 网络协议规范 LoRaWAN 1.1 版本封稿很久了也没有完整啃过一遍,最近边啃边翻译,趁着这个机会把它码下来. 如果觉得哪里有问题,欢迎留言斧正. 翻译不易,转载请申明出处和 ...
- 给Linux系统管理员准备的Nmap命令的29个实用范例
map即网络映射器对Linux系统/网络管理员来说是一个开源且非常通用的工具.Nmap用于在远程机器上探测网络,执行安全扫描,网络审计和搜寻开放端口.它会扫描远程在线主机,该主机的操作系统,包过滤器和 ...