父控制器
#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(0, 10, 0, 10)); 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==0) { width=[key sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(self.bounds.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping].width+20; } CGRectDivide(hrect, &vrect, &hrect, width, CGRectMinXEdge); UILabel *lb=[UILabel LabWithFrame:UIEdgeInsetsInsetRect(vrect, UIEdgeInsetsMake(0, 10, 0, 0)) text:[NSString stringWithFormat:@"%@",key] textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:12]]; [self addSubview:lb]; lb=[UILabel LabWithFrame:UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(0, 5, 0, 10)) text:value textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:12]]; [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(0, 10, 0, 10)); FyGroupLineView *lineView=[[FyGroupLineView alloc] initWithFrame:rect WithLineNumbers:textArr.count]; [self addSubview:lineView]; [lineView release]; for (int i=0; i<textArr.count; i++) { CGRectDivide(rect, &hrect, &rect,height, CGRectMinYEdge); CGRectDivide(hrect, &vrect, &hrect, width, CGRectMinXEdge); UILabel *lb=[UILabel LabWithFrame:UIEdgeInsetsInsetRect(vrect, UIEdgeInsetsMake(0, 10, 0, 0)) text:[NSString stringWithFormat:@"%@:",textArr[i]] textColor:[UIColor blackColor] textAlign:NSTextAlignmentLeft font:[UIFont systemFontOfSize:12]]; [self addSubview:lb]; int flag=[flagArr[i] intValue];//對應控件的標志 if (flag!=3) { UIImageView *imgview=[UIImageView ImageViewImageName:@"txField_image.png" frame:UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(5, 0, 5, 10))]; [self addSubview:imgview]; } NSString *placeHolder=(isShow==YES)?[NSString stringWithFormat:@"請輸入%@",textArr[i]]:@""; UITextField *txt=nil; CGRect rightRect=UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(5, 10, 5, 15)); if (flag ==0){ //文本框 鍵盤返回類型 UIKeyboardTypeNamePhonePad, if (i==textArr.count-1) 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==1){//文本框 鍵盤返回類型 UIKeyboardTypePhonePad, if (i==textArr.count-1) 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==2){//文本框 鍵盤返回類型 電子郵件 if (i==textArr.count-1) 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==3)//按鈕 時間控件DatePicker txt=(UITextField *)[UIButton ButtonWithImageName:@"txField_image.png" hImageName:@"txField_image.png" frame:UIEdgeInsetsInsetRect(hrect, UIEdgeInsetsMake(5, 0,5, 10)) title:[NSString stringWithFormat:@"請選擇%@",textArr[i]] titleColor:[UIColor blackColor] font:[UIFont systemFontOfSize:12] target:delegate action:method]; txt.tag=tag+i; if (i==textArr.count-1) { 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(200, 207, 216).CGColor; background.layer.cornerRadius = 5; background.layer.masksToBounds = YES; [self addSubview:background]; [background release]; CGRect vRect = self.bounds,lineRect; for (int i = 0; i<cols-1; i++) { CGRectDivide(vRect, &lineRect, &vRect, self.bounds.size.height/cols, CGRectMinYEdge); lineRect = UIEdgeInsetsInsetRect(lineRect, UIEdgeInsetsMake(lineRect.size.height-0.5, 0, -0.5, 0)); UILabel *lineLab = [UILabel LabWithFrame:lineRect text:nil textColor:nil textAlign:NSTextAlignmentCenter font:[UIFont systemFontOfSize:11]]; lineLab.backgroundColor = rgb(200, 207, 216); [self addSubview:lineLab]; } } return self; }