UITextField自定义键盘 ---->> 添加光标

声明:此方法我在那里看见过,不是原创,忘记以前看见过的地址了,由于最近整理一下东西,觉得这个有必要blog 一下

光标 可以左右 移动

UITextField自定义键盘 ---->> 添加光标_第1张图片
C70196F19ACB968A99D66C8A1C5E735D.png
UITextField自定义键盘 ---->> 添加光标_第2张图片
A5B0B790E91617E011646218C2FB3689.png
UITextField自定义键盘 ---->> 添加光标_第3张图片
4C3FF2E8BC9E0F7E018EE5F21DC879E6.png

//隐藏系统键盘
_inputTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];

1)BaseTextField 基类
1> 禁止粘贴 选择 等等


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}

2> 第二种方法

/

//// if(action ==@selector(paste:))//禁止粘贴
//// return NO;
//// if(action ==@selector(select:))// 禁止选择
//// return NO;
//// if(action ==@selector(selectAll:))// 禁止全选
//// return NO;
////
//// return[super canPerformAction:action withSender:sender];
//
*/

2)UITextField 的 category 主要实现
.h 这两个方法
//获取光标的位置

  • (NSRange) selectedRange;
    //设置光标位置
  • (void) setSelectedRange:(NSRange) range;

.m 文件
 
  
  • (NSRange) selectedRange{
    UITextPosition* beginning = self.beginningOfDocument;
    UITextRange* selectedRange = self.selectedTextRange;
    UITextPosition* selectionStart = selectedRange.start;
    UITextPosition* selectionEnd = selectedRange.end;
    const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
    const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
    return NSMakeRange(location, length);
    }
    // 备注:UITextField必须为第一响应者才有效
  • (void) setSelectedRange:(NSRange) range {
    UITextPosition* beginning = self.beginningOfDocument;
    UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
    UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
    UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
    [self setSelectedTextRange:selectionRange];
    }
  • //使用的代码

    import "ViewController.h"

    import "UITextField+ExtentRange.h"

    @interface ViewController ()
    {

    __weak IBOutlet UITextField *_cardNumberTextField;
    

    }
    @end
    @implementation ViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
      _cardNumberTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
      [_cardNumberTextField becomeFirstResponder];
      _cardNumberTextField.delegate = self;
      _cardNumberTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];
      [_cardNumberTextField setSelectedRange:NSMakeRange(0, 0)];
      }

    • (IBAction)pp:(id)sender {
      UIButton *button = (UIButton *)(sender);
      //获取光标的位置
      NSRange range = [_cardNumberTextField selectedRange];
      //添加
      [_cardNumberTextField insertText:button.titleLabel.text];
      //设置光标位置
      [_cardNumberTextField setSelectedRange:NSMakeRange(range.location + 1, 0)];
      }

    • (IBAction)deleteStr:(id)sender {
      if (_cardNumberTextField.text.length < 1 || [_cardNumberTextField.text isEqualToString:@""]) {
      return;
      }

      NSRange range = [_cardNumberTextField selectedRange];
      if (range.location == 0) {
      if (_cardNumberTextField.text.length > 0) {
      [_cardNumberTextField setSelectedRange:NSMakeRange(_cardNumberTextField.text.length, 0)];

            NSRange range2 = [_cardNumberTextField selectedRange];
            [_cardNumberTextField deleteBackward];
            range2.location-=1;
            [_cardNumberTextField setSelectedRange:NSMakeRange(range2.location, 0)];
            
            return;
        }else{
            return;
        }
      

      }
      [_cardNumberTextField deleteBackward];
      range.location-=1;
      [_cardNumberTextField setSelectedRange:NSMakeRange(range.location, 0)];
      }

    你可能感兴趣的:(UITextField自定义键盘 ---->> 添加光标)