TextField和TextView两种输入时效果
2016-01-26 本文已影响1812人
SPIREJ
图0 - 示例代码主界面
-
第一种,三方RPFloatingPlaceholders。用RPFloatingPlaceholderTextField 或者 RPFloatingPlaceholderTextView创建的textField 或 textView会得到这种效果。
图1 - 第一种输入时的效果
创建一个 textField
CGRect frame = CGRectMake(20.f, 300.f, 273.f, 30.f);
RPFloatingPlaceholderTextField *flTextField =[[RPFloatingPlaceholderTextField alloc] initWithFrame:frame];
//弹上去时小字颜色
flTextField.floatingLabelActiveTextColor = [UIColor blueColor];
//placeholder 颜色
flTextField.floatingLabelInactiveTextColor = [UIColor grayColor];
flTextField.placeholder = @"您的账号";
flTextField.font = [UIFont fontWithName:@"Helvetica" size:16.f];
//另一种效果 默认为RPFloatingPlaceholderAnimateUpward
// flTextField.animationDirection = RPFloatingPlaceholderAnimateDownward; // You can change animation direction
// flTextField.text = @"I love lamp."; // You can set text after it's been initialized
[self.view addSubview:flTextField];
创建一个textView
CGRect frame2 = CGRectMake(20.f, 350.f, 273.f, 95.f);
RPFloatingPlaceholderTextView *flTextView = [[RPFloatingPlaceholderTextView alloc] initWithFrame:frame2];
flTextView.floatingLabelActiveTextColor = [UIColor blueColor];
flTextView.floatingLabelInactiveTextColor = [UIColor grayColor];
flTextView.placeholder = @"Tell me about yourself";
flTextView.font = [UIFont fontWithName:@"Helvetica" size:16.f];
flTextView.animationDirection = RPFloatingPlaceholderAnimateDownward;
flTextView.text = @"I love lamp. This is pre-existing text."; // You can set text after it's been initialized
[self.view addSubview:flTextView];
-
第二种,和第一种意思差不多,个人觉得第二种效果稍微好一点。三方类:RPFloatingPlaceholders
图2 - 第二种输入时的效果
创建一个textField
//初始化一个textField
JVFloatLabeledTextField *titleField = [[JVFloatLabeledTextField alloc] initWithFrame:CGRectMake(50, 200, kDeviceWidth-100, 30)];
//textField类型
titleField.borderStyle = UITextBorderStyleRoundedRect;
titleField.font = [UIFont systemFontOfSize:16];
//placeholder 的属性设置
titleField.attributedPlaceholder =[[NSAttributedString alloc] initWithString:NSLocalizedString(@"Title", @"")attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
//弹上去的字体大小、颜色
titleField.floatingLabelFont = [UIFont boldSystemFontOfSize:10];
titleField.floatingLabelTextColor = [UIColor brownColor];
//是否有清除按钮
titleField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:titleField];
//这个属性是约束Autosizing控制,当打开约束的时候,要约束条件完全,否则可能试图丢失。
// titleField.translatesAutoresizingMaskIntoConstraints = NO;
titleField.keepBaseline = YES;
// [titleField becomeFirstResponder];
创建一个textView
//初始化textView
JVFloatLabeledTextView *descriptionField = [[JVFloatLabeledTextView alloc] initWithFrame:CGRectMake(50, 300, kDeviceWidth-100, 100)];
//textView placeholder字符串,字体大小,颜色
descriptionField.font = [UIFont systemFontOfSize:16];
descriptionField.placeholder = NSLocalizedString(@"Description hello my baby", @"");
descriptionField.placeholderTextColor = [UIColor darkGrayColor];
//弹上去时的大小,颜色
descriptionField.floatingLabelFont = [UIFont boldSystemFontOfSize:10];
descriptionField.floatingLabelTextColor = [UIColor brownColor];
[self.view addSubview:descriptionField];
descriptionField.translatesAutoresizingMaskIntoConstraints = NO;
-
第三,textField改变btn状态
图3 - textField控制btn状态
~3.1 初始设置btn 灰色不可点击
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_gray"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = NO;
~3.2 实现textField 代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *currentStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self changeButtonStatus:currentStr TF:textField _TF1:_nameTF _TF2:_phoneTF];
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_gray"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = NO;
return YES;
}
~3.3两个textField时的判定方法,当然多个textField时也是一个道理
- (void)changeButtonStatus:(NSString *)str TF:(UITextField *)textField _TF1:(UITextField *)tf1 _TF2:(UITextField *)tf2
{
if (textField == tf1) {
if ([str length] != 0) {
if (tf2.text.length != 0) {
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_blue"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = YES;
}else {
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_gray"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = NO;
}
}else {
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_gray"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = NO;
}
}else if (textField == tf2) {
if ([str length] != 0) {
if (tf1.text.length != 0) {
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_blue"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = YES;
}else {
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_gray"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = NO;
}
}else{
[_finishedBtn setBackgroundImage:[UIImage imageNamed:@"btn_gray"] forState:UIControlStateNormal];
_finishedBtn.userInteractionEnabled = NO;
}
}
}
#######示例代码链接:https://github.com/SPIREJ/SJTextFieldDemo