一:编辑被键盘遮挡的问题
参考自:http://blog.csdn.net/windkisshao/article/details/21398521
1.自定方法 ,用于移动视图
-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval;
2.注册监听
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
3.实现方法
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
if(nil==self.myTextView) return;// self.editTextView 为被键盘遮挡住的控件
CGRect rect = self.myTextView.frame;
float textY = rect.origin.y + rect.size.height;
float bottomY = SCREENHEIGHT - textY;//得到下边框到底部的距离 SCREENHEIGHT 为当前设备的高度
if(bottomY >=keyboardRect.size.height ){//键盘默认高度,如果大于此高度,则直接返回
return;
}
float moveY = keyboardRect.size.height - bottomY;
[self moveInputBarWithKeyboardHeight:moveY withDuration:animationDuration];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
}
-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval{
CGRect rect1 = self.view.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:_NSTimeInterval];
rect1.origin.y = -_CGRectHeight;//view往上移动
self.view.frame = rect1;
[UIView commitAnimations];
}
二: 键盘
(1)键盘类型
UIKeyboardTypeDefault, // 默认键盘:支持所有字符
- UIKeyboardTypeASCIICapable, // 支持ASCII的默认键盘
- UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,支持+*#等符号
- UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符
- UIKeyboardTypeNumberPad, //纯数字键盘 (不带小数点)
- UIKeyboardTypeDecimalPad //数字键盘 (带小数点)
- UIKeyboardTypePhonePad, // 电话键盘
- UIKeyboardTypeNamePhonePad, // 电话键盘,也支持输入人名字
- UIKeyboardTypeEmailAddress, // 用于输入电子邮件地址的键盘
- UIKeyboardTypeWebSearch //用于搜索
- UIKeyboardTypeAlphabet
如: self.uIphone.keyboardType = UIKeyboardTypeNumberPad;
祥见:http://blog.csdn.net/crazyzhang1990/article/details/39965931
(2) return键的类型
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮 (完成,可用于填写资料的最后一项)
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮 (可用于登录/注册/填写地址-->下一步)
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮 (可用于搜索)
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
如:
self.uIphone.keyboardType = UIKeyboardTypeNumberPad;
(3) 点击return建响应事件
A.UITextField --> - (BOOL)textFieldShouldReturn:(UITextField *)textField{
如: 添加地址
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if(textField.tag==10) //下一步 (姓名)
{
[self.uName resignFirstResponder];
[self.uIphone becomeFirstResponder];
}else if(textField.tag==11) //下一步 (电话)
{
[self.uIphone resignFirstResponder];
[self.reciveAddress becomeFirstResponder];
}else if(textField.tag==100) //完成(地址填完之后可直接调用接口)
{
[self.reciveAddress resignFirstResponder];
[self addBtn:nil];
}
return YES;
}
B.UITextView --> - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
如:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return
//在这里做你响应return键的代码
[self addBtn:nil];
return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
}
return YES;
}
参考自:http://blog.sina.com.cn/s/blog_59fb90df010176re.html
三:UITextField小结
1.自定义UITextField 左边加图标(如:登录)
UIImageView *i=[[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 21, 21)];
[i setImage:[UIImage imageNamed:@"yh"]];
UIView *userLeftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 38)];
[userLeftView addSubview:i];
self.userName.leftView=userLeftView;
self.userName.leftViewMode=UITextFieldViewModeAlways;
2.设置自定义UITextField 的Placeholder颜色
UIColor *color = [UIColor blue];
self.userName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 手机号" attributes:@{NSForegroundColorAttributeName: color}];
四:UITextView小结
1.设置Placeholder
@property (nonatomic,strong) UILabel *proText1;
self.automaticallyAdjustsScrollViewInsets = NO;
[self.leaveMessage setDelegate:self];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(self.leaveMessage.frame.origin.x+10, self.leaveMessage.frame.origin.y-35, self.leaveMessage.bounds.size.width, 100)];
lbl.text=@" 感谢您留下宝贵的意见....";
[lbl setFont:[UIFont systemFontOfSize:15.0]];
lbl.enabled=NO;
self.proText1=lbl;
[self.view addSubview:lbl];
#pragma mark -----textView的代理事件
-(void)textViewDidChange:(UITextView *)textView
{
// textView.text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (textView.text.length == 0) {
self.proText1.text = @" 感谢您留下宝贵的意见....";
}else{
self.proText1.text = @"";
}
}
2.去掉空格以及换行
NSString *content = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
有的时候由于不正确的操作,直接将textView.text作为参数传 到服务器,可能会产生意想不到的错误,因此就需要加这个对字符串进行一下处理
更多请参考:http://www.41443.com/HTML/iphone/20141109/204260.html