這篇文章給大家分享的是有關(guān)iOS中輸入框如何設(shè)置指定字符輸入功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

一、只能輸入純數(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:@"/upload/otherpic43/"];
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 @"/upload/otherpic43/"
接著
- (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 @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/upload/otherpic43/"
同樣道理具體能夠輸入那些內(nèi)容如果輸入內(nèi)容能夠一一列舉的話我們就可以通過define來設(shè)置了,實(shí)現(xiàn)起來超簡(jiǎn)單。
限制只能輸入中文
在這里用到了觀察者(更多觀察者模式的介紹參考這里:https://www.jb51.net/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];
}
}感謝各位的閱讀!關(guān)于“iOS中輸入框如何設(shè)置指定字符輸入功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:iOS中輸入框如何設(shè)置指定字符輸入功能-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.chinadenli.net/article16/deiddg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈、網(wǎng)站排名、用戶體驗(yàn)、外貿(mào)網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容