二哈吃树叶.gif
@property (nonatomic, strong) ATextFieldPlaceholder *textField; ///< 输入
@property (nonatomic, strong) NSString *phoneContent;
// 输入框
- (ATextFieldPlaceholder *)textField
{
if (!_textField) {
_textField = [[ATextFieldPlaceholder alloc] init];
_textField.placeholder = @"188 8888 8888";
_textField.placeholderColor = color_F2F2F2;
_textField.placeholderFont = AFONTMedium(24);
_textField.tf_delegate = self;
_textField.font = AFONTMedium(24);
_textField.textColor = color_333333;
_textField.returnKeyType = UIReturnKeyDone;
_textField.keyboardType = UIKeyboardTypeNumberPad;
_textField.backgroundColor = [UIColor clearColor];
_textField.delegate = self;
_textField.textAlignment = NSTextAlignmentLeft;
[_textField addTarget:self action:@selector(onAcionCodeFieldChanged:) forControlEvents:UIControlEventEditingChanged];
[_textField addDoneOnKeyboardWithTarget:self action:@selector(ActionSearchDid)];
}
return _textField;
}
- (void)onAcionCodeFieldChanged:(UITextField *)textField
{
NSString *currentText = textField.text;
// 是否正在执行删除操作
BOOL editFlag = NO;
if (currentText.length < self.phoneContent.length) {
editFlag = YES;
}
// 光标位置
NSInteger targetPosition = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];
textField.text = [currentText isPhone];
NSInteger currentPosition = targetPosition;
// 删除
if (editFlag)
{
if (currentPosition == 4 || currentPosition == 9)
{
currentPosition -= 1;
}
}
else
{
if (currentPosition == 4 || currentPosition == 9)
{
currentPosition += 1;
}
}
UITextPosition *nowPosition = [textField positionFromPosition:textField.beginningOfDocument offset:currentPosition];
textField.selectedTextRange = [textField textRangeFromPosition:nowPosition toPosition:nowPosition];
if (textField.text.length == 11+2)
//if ([AStrUtils iPhoneRegular11phone:textField.text])
{
[self endEditing:YES];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(onServePhoneReplenishingCell:inputText:)]) {
[self.delegate onServePhoneReplenishingCell:self inputText:textField.text];
}
}
// MARK: ----- UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (self.textField == textField) {
self.phoneContent = textField.text;
if (self.phoneContent.length >= 13 && string.length > 0) {
NSLog(@"手机号码为11位");
return NO;
}
//self.previousSelection = textField.selectedTextRange;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[self ActionSearchDid];
return YES;
}
// MARK: ----- 点击事件
- (void)ActionBenBenClick
{
if (self.delegate && [self.delegate respondsToSelector:@selector(onServePhoneReplenishingCell:didbenben:)]) {
[self.delegate onServePhoneReplenishingCell:self didbenben:YES];
}
}
- (void)ActionSearchDid
{
[self endEditing:YES];
if (self.textField.text.length == 11+2)
//if ([AStrUtils iPhoneRegular11phone:self.textField.text])
{
if (self.delegate && [self.delegate respondsToSelector:@selector(onServePhoneReplenishingCell:inputText:)]) {
[self.delegate onServePhoneReplenishingCell:self inputText:self.textField.text];
}
}
else
{
[ViewTool tipWithContent:@"请输入11位手机号"];
}
}
扩展
- (NSString *)isPhone {
NSString * str = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableString * phone = [NSMutableString stringWithString:str];
if (phone.length <= 3) {
} else if (phone.length <= 7) {
[phone insertString:@" " atIndex:3];
} else if (phone.length <= 11) {
[phone insertString:@" " atIndex:7];
[phone insertString:@" " atIndex:3];
}
return phone;
}
参考区域
参考1
网友评论