中文的 UTF8 是 3 字节,Emoj 是 4 个字节、占 2 个字符,所有不能简单的用length来判断
本文对于字符和字节的输入限制各提供了两种方法
字符限制
- 方法一
- (void)textFieldDidChange:(UITextField *)textField
{
NSString *text = textField.text;
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
// 中文输入下,选择没有高亮的字
if (!position)
{
if (textField.text.length > MAX_STRING_LENGTH)
{
NSRange rangeIndex = [text rangeOfComposedCharacterSequenceAtIndex:MAX_STRING_LENGTH];
if (rangeIndex.length == 1)
{
textField.text = [text substringToIndex:MAX_STRING_LENGTH];
}
else
{
// emoji占两个字符 防止整词被切断
NSRange rangeRange = [text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STRING_LENGTH)];
if (rangeRange.length > MAX_STRING_LENGTH)
{
rangeRange.length = rangeRange.length - rangeIndex.length;
}
textField.text = [text substringWithRange:rangeRange];
}
}
}
}
- 方法二
- (void)textFieldDidChange:(UITextField *)textField
{
NSString *text = textField.text;
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position)
{
if (text.length > MAX_LENGTH)
{
NSRange range;
NSUInteger inputLength = 0;
for(int i=0; i < text.length && inputLength <= MAX_LENGTH; i += range.length)
{
range = [textField.text rangeOfComposedCharacterSequenceAtIndex:i];
inputLength += [text substringWithRange:range].length;
if (inputLength > MAX_LENGTH)
{
textField.text = [text substringWithRange:NSMakeRange(0, range.location)];
}
}
}
}
}
字节限制
- 方法一
- (void)textFieldDidChange:(UITextField *)textField
{
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position)
{
while ([textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > MAX_STRING_LENGTH)
{
NSRange rangeIndex = [textField.text rangeOfComposedCharacterSequenceAtIndex:textField.text.length - 1];
if (rangeIndex.length == 1)
{
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
else
{
NSRange rangeRange = [textField.text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, textField.text.length - 1)];
if (rangeRange.length > textField.text.length - 1)
{
rangeRange.length = rangeRange.length - rangeIndex.length;
}
textField.text = [textField.text substringWithRange:rangeRange];
}
}
}
}
- 方法二
- (void)textFieldDidChange:(UITextField *)textField
{
NSString *text = textField.text;
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position){
NSUInteger textBytesLength = [textField.text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if (textBytesLength > MAX_BYTES_LENGTH) {
NSRange range;
NSUInteger byteLength = 0;
for(int i=0; i < text.length && byteLength <= MAX_BYTES_LENGTH; i += range.length) {
range = [textField.text rangeOfComposedCharacterSequenceAtIndex:i];
byteLength += strlen([[text substringWithRange:range] UTF8String]);
if (byteLength > MAX_BYTES_LENGTH) {
textField.text = [text substringWithRange:NSMakeRange(0, range.location)];
}
}
}
}
}
网友评论