iOS 設置輸入框的指定字符輸入
阿新 • • 發佈:2018-01-12
efi nss cto tostring lai object options ldr epo
一、只能輸入純數字
在這裏以UITextField為例:其實現代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return [self validateNumber:string];
}
- (BOOL)validateNumber:(NSString*)number { BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@ "0123456789" ];
int i = 0 ;
while (i < number.length) {
NSString * string = [number substringWithRange:NSMakeRange(i, 1 )];
NSRange range = [string rangeOfCharacterFromSet:tmpSet]; if (range.length == 0 ) {
res = NO;
break ;
}
i++;
}
return res;
}
|
另外我們還有一種更加簡便的方法來實現這一目的:
首先宏定義
1 |
#define NUMBER @ "0123456789"
|
接著
1 2 3 4 5 6 |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBER] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@ "" ];
return [string isEqualToString:filtered];
}
|
二、只能輸入純大小寫字母
和以上只能輸入純數字類似,實現起來簡單,只需要宏定義
1 |
#define LETTER @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
然後實現
1 2 3 4 5 6 |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:LETTER] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@ "" ];
return [string isEqualToString:filtered];
}
|
三、大小寫字母和數字結合輸入
對照以上
1 |
#define LETTER_NUMBER @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
同樣道理具體能夠輸入那些內容如果輸入內容能夠一一列舉的話我們就可以通過define來設置了,實現起來超簡單。
限制只能輸入中文
在這裏用到了觀察者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
- ( void )viewDidLoad {
[ super viewDidLoad];
_myTextField.delegate = self;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:UITextFieldTextDidChangeNotification object:_myTextField];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
//過濾非漢字字符
textField.text = [self filterCharactor:textField.text withRegex:@ "[^\u4e00-\u9fa5]" ];
if (textField.text.length >= 4 ) {
textField.text = [textField.text substringToIndex: 4 ];
}
return NO;
}
- ( void )textFiledEditChanged:(id)notification{
UITextRange *selectedRange = _myTextField.markedTextRange;
UITextPosition *position = [_myTextField positionFromPosition:selectedRange.start offset: 0 ];
if (!position) { //// 沒有高亮選擇的字
//過濾非漢字字符
_myTextField.text = [self filterCharactor:_myTextField.text withRegex:@ "[^\u4e00-\u9fa5]" ];
if (_myTextField.text.length >= 4 ) {
_myTextField.text = [_myTextField.text substringToIndex: 4 ];
}
} else { //有高亮文字
//do nothing
}
}
- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
NSString *searchText = string;
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
NSString *result = [regex stringByReplacingMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange( 0 , searchText.length) withTemplate:@ "" ];
return result;
}
- ( void )dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
|
如果要限制輸入字符位數的話可以直接設置,這個實現上有很多種,最簡單的就是
1 2 3 4 5 6 |
- ( void )textViewDidChange:(UITextView *)textView{
NSInteger number = [textView.text length];
if (number > 300 ) {
textView.text = [textView.text substringToIndex: 300 ];
}
}
|
iOS 設置輸入框的指定字符輸入