声明:此方法我在那里看见过,不是原创,忘记以前看见过的地址了,由于最近整理一下东西,觉得这个有必要blog 一下
光标 可以左右 移动
//隐藏系统键盘
_inputTextField.inputView = [[UIView alloc]initWithFrame:CGRectZero];
1)BaseTextField 基类
1> 禁止粘贴 选择 等等
<pre>
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
</pre>
2> 第二种方法
<pre>
/
//// if(action ==@selector(paste:))//禁止粘贴
//// return NO;
//// if(action ==@selector(select:))// 禁止选择
//// return NO;
//// if(action ==@selector(selectAll:))// 禁止全选
//// return NO;
////
//// return[super canPerformAction:action withSender:sender];
//
*/
</pre>
2)UITextField 的 category 主要实现
.h 这两个方法
//获取光标的位置
<pre>
- (NSRange) selectedRange;
//设置光标位置 - (void) setSelectedRange:(NSRange) range;
</pre>
.m 文件
<pre> - (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];
}
</pre>
//使用的代码
<pre>
import "ViewController.h"
import "UITextField+ExtentRange.h"
@interface ViewController ()<UITextFieldDelegate>
{
__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)];
}
</pre>