iOS开发 输入字数计算、限制
更新日期:
Summary
This is a introduction of caculating or limiting text word counts.
The text is divided into DBC case charaters and SBC case charaters.
本文介绍全角字符和忽略全角字符的文本字数统计或限制。
Tip
Editting in UITextField,these codes should be use for the protocol method
“- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString )string”,while UITextView is
“- (BOOL)textView:(UITextView )textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString )text”.在UITextField和UITextView编辑时,以下代码分别用在其”输入许可”的代理方法shouldChangeCharactersInRange
和shouldChangeTextInRange中。
DBC case (Half-width) characters
- Considering NSString objctive’s length,such as string.length, as word counts.
不考虑全角字符
if (range.location >= wordCount && string.length) {
return NO;
}else if (string.length + range.location > wordCount) {
textField.text = [textField.text stringByAppendingString:[string substringToIndex:wordCount - range.location]];
return NO;
}
return YES;
SBC case (Full-width) charaters
- A Full-width charater is counted two byte,a Half-width charater is counted one
byte.
区分全角、半角计算文本字数。
The limitCount is a const integer value which is the max count that user can input.
limitCount是允许用户输入的最大字数。
if (!text.length) {//backspace
return YES;
}
int count = [self wordCountWithText:[textView.text stringByAppendingString:text]];//charater counts
if (count >= limitCount) {
count = limitCount - [self wordCountWithText:textView.text];//The remaining words
textView.text = [textView.text stringByAppendingString:[text substringToIndex:count / 2]];
_lbCount.text = @"0";
return NO;
}
return YES;
- (int)wordCountWithText:(NSString*)text {
if (!text || !text.length) {
return 0;
}
int length = 0;
char *point = (char*)[text cStringUsingEncoding:NSUnicodeStringEncoding];
NSUInteger lengthEncode = [text lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
for (NSUInteger index = 0; index < lengthEncode; index++) {
if (*point) {
length++;
}
point++;
}
return length;
}
- Another way below is caculating for the Full-width charaters count:
下面的方法是计算包含全角字符的文本字数:
#import "RegexKitLite.h"
- (int)calcStrWordCount:(NSString *)str {
int nResult = 0;
NSString *strSourceCpy = [str copy];
NSMutableString *strCopy =[[NSMutableString alloc] initWithString: strSourceCpy];
NSArray *array = [strCopy componentsMatchedByRegex:@"((news|telnet|nttp|file|http|ftp|https)://){1}"
"(([-A-Za-z0-9]+(\\.[-A-Za-z0-9]+)*(\\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\\.[0-9]{1,3}){3}))(:[0-9]*)"
"?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*)*"];
if ([array count] > 0) {
for (NSString *itemInfo in array) {
NSRange searchRange = {0};
searchRange.location = 0;
searchRange.length = [strCopy length];
[strCopy replaceOccurrencesOfString:itemInfo withString:@" " options:NSCaseInsensitiveSearch range:searchRange];
}
}
char *pchSource = (char *)[strCopy cStringUsingEncoding:NSUTF8StringEncoding];
int sourcelen = strlen(pchSource);
int nCurNum = 0; //current word count
for (int n = 0; n < sourcelen; ) {
if( *pchSource & 0x80 ) {
pchSource += 3; // NSUTF8StringEncoding encoding Chinese words is counted as 3 bytes
n += 3;
nCurNum += 2;
}
else {
pchSource++;
n += 1;
nCurNum += 1;
}
}
//the last word that insuffices one is calculated for one
nResult = nCurNum / 2 + nCurNum % 2;
[strSourceCpy release];
[strCopy release];
return nResult;
}
The limit word count codes above compatible with users’ paste action.
以上限制字数代码在用户粘贴时也有效。
编写本博客相关环境:Mac OS 10.11.3,Mou 0.8.7,Xcode 7.2 (7C68),iOS 9.2
转自大彬的 iOS开发 输入字数计算、限制
欢迎大家关注小编的博客 大彬的博客