iOS中輸入框設(shè)置指定字符輸入的方法
前言
對(duì)于開發(fā)者來說,在很多情況下,一般的輸入框需要按照要求進(jìn)行輸入,輸入內(nèi)容由開發(fā)人員來指定。例如:密碼輸入框只能輸入純數(shù)字或者是拼音與數(shù)字結(jié)合的文本等,那么我們?cè)陂_發(fā)的時(shí)候就需要做一些輸入文本的限時(shí)。下面話不多說了,來一起看看詳細(xì)的介紹吧。
一、只能輸入純數(shù)字
在這里以UITextField為例:其實(shí)現(xiàn)代碼如下:
- (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;
}
另外我們還有一種更加簡(jiǎn)便的方法來實(shí)現(xiàn)這一目的:
首先宏定義
#define NUMBER @"0123456789"
接著
- (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];
}
二、只能輸入純大小寫字母
和以上只能輸入純數(shù)字類似,實(shí)現(xiàn)起來簡(jiǎn)單,只需要宏定義
#define LETTER @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
然后實(shí)現(xiàn)
- (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];
}
三、大小寫字母和數(shù)字結(jié)合輸入
對(duì)照以上
#define LETTER_NUMBER @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
同樣道理具體能夠輸入那些內(nèi)容如果輸入內(nèi)容能夠一一列舉的話我們就可以通過define來設(shè)置了,實(shí)現(xiàn)起來超簡(jiǎn)單。
限制只能輸入中文
在這里用到了觀察者(更多觀察者模式的介紹參考這里:http://www.dbjr.com.cn/article/76122.htm)
- (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];
}
如果要限制輸入字符位數(shù)的話可以直接設(shè)置,這個(gè)實(shí)現(xiàn)上有很多種,最簡(jiǎn)單的就是
- (void)textViewDidChange:(UITextView *)textView{
NSInteger number = [textView.text length];
if (number > 300) {
textView.text = [textView.text substringToIndex:300];
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
IOS開發(fā)中使用UIFont設(shè)置字體及批量創(chuàng)建控件
這篇文章主要介紹了IOS開發(fā)中使用UIFont設(shè)置字體及批量創(chuàng)建控件的方法,內(nèi)容很實(shí)用,感興趣的小伙伴們可以參考一下2016-03-03
iOS簡(jiǎn)單抽屜效果的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了iOS簡(jiǎn)單抽屜效果的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼
這篇文章主要介紹了iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
iOS開發(fā)實(shí)戰(zhàn)之Label全方位對(duì)齊的輕松實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)實(shí)戰(zhàn)之輕松實(shí)現(xiàn)Label全方位對(duì)齊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10

