- 限制只能輸入數(shù)字并且限制輸入位數(shù).要先實(shí)現(xiàn)限制自能輸入0-9 的數(shù)字的方法. 如果只在textfiled設(shè)置彈出鍵盤是數(shù)字鍵盤的話第三方輸入法還是能輸入數(shù)字之外的字符
實(shí)現(xiàn)限制只能輸入數(shù)字的方法
* 限制textfiled只能輸入0-9數(shù)字
*
* @param number string
*
* @return YES/NO
*/
- (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;
}
實(shí)現(xiàn)UITextFile的delegate方法,調(diào)用只能輸入數(shù)字的方法.并限制其位數(shù)**
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (range.location >= 2){ //限制位數(shù).
return NO;
}else{//限制只能輸入數(shù)字
return [self validateNumber:string];
}
}
- 限制實(shí)現(xiàn)中文兩個(gè)字符. 英文占一個(gè)字符. 如限制英文輸入只能18個(gè),那中文只能輸入9個(gè)
監(jiān)聽(tīng)通知textfiled正在didchange的通知**
//添加監(jiān)聽(tīng)textFile文字 改變的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:self.remarkTextField];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
name:@"UITextFieldTextDidChangeNotification"
object:self.nameTextField];
- (void)dealloc{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:self.nameTextField];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:self.remarkTextField];
WSLog(@"%s",__func__);
}
實(shí)現(xiàn)計(jì)算中英文混輸字節(jié)計(jì)算
/**
* 計(jì)算中英文混輸字節(jié)數(shù)
*
* @param s 字符串
*
* @return 返回字節(jié)數(shù)
*/
- (CGFloat)countW:(NSString *)s
{
int i;CGFloat n=[s length],l=0,a=0,b=0;
CGFloat wLen=0;
unichar c;
for(i=0;i<n;i++){
c=[s characterAtIndex:i];//按順序取出單個(gè)字符
if(isblank(c)){//判斷字符串為空或?yàn)榭崭? b++;
}else if(isascii(c)){
a++;
}else{
l++;
}
wLen=l+(CGFloat)((CGFloat)(a+b)/2.0);
WSLog(@"wLen--%f",wLen);
if (wLen>=MaxLength - 0.5 &&wLen<MaxLength + 0.5) {//設(shè)定這個(gè)范圍是因?yàn)橄熬睿?dāng)輸入了當(dāng)輸入9英文渠抹,即4.5蝙昙,后面還能輸1字母,但不能輸1中文
_subLen=l+a+b;//_subLen是要截取字符串的位置
}
}
if(a==0 && l==0)
{
_subLen=0;
return 0;//只有isblank
}
else{
return wLen;//長(zhǎng)度梧却,中文占1奇颠,英文等能轉(zhuǎn)ascii的占0.5
}
}
實(shí)現(xiàn)textdidchange的通知方法**
-(void)textFiledEditChanged:(NSNotification *)obj{
@try{
UITextField * textField =(UITextField *) obj.object;
NSString *str = [[textField text] stringByReplacingOccurrencesOfString:@"?" withString:@""];//輸入的字符,包括鍵盤上高亮的未轉(zhuǎn)成中文的拼音
NSLog(@"str--%@",str);
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];//高亮
if (!position) {//沒(méi)高亮的文字
CGFloat ascLen=[self countW:str];//沒(méi)高亮放航,獲取長(zhǎng)度
NSLog(@"ascLen------------------%f",ascLen);
if (ascLen > MaxLength) {
NSString *strNew = [NSString stringWithString:str];
NSLog(@"strNew--%@",strNew);
NSLog(@"_subLen%ld",(long)_subLen);
if (_subLen==0) {
_subLen=strNew.length;
}
[textField setText:[strNew substringToIndex:_subLen]];
}
}
else{
// NSLog(@"輸入的英文還沒(méi)轉(zhuǎn)化為漢字");//只是高亮烈拒,不應(yīng)該算長(zhǎng)度
}
}
@catch(NSException *exception) {
NSLog(@"exception:%@", exception);
}
@finally {
}
}