cdts/xdts-ios 3/TreeHole/Code/Utility/TextInputLimit/NSString+MTLimit.m
2023-07-27 09:20:00 +08:00

131 lines
5.0 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NSString+MTLimit.m
// Meet
//
// Created by ko1o on 2018/10/22.
// Copyright © 2018年 ko1o. All rights reserved.
//
#import "NSString+MTLimit.h"
@implementation NSString (MTLimit)
- (NSInteger)mt_customByteLength
{
//拿到所有str的length长度包括中文英文..都算为1个字符
NSUInteger len = self.length;
//汉字字符集
// NSString* pattern = @"[\u4e00-\u9fa5]";
// NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
// //计算中文字符的个数
// NSInteger numMatch = [regex numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, len)];
// //计算中文2个字符英文1个字符
// return len + numMatch;
//两个英文字符算1个长度,中文的话默认也是1,emoj表情返回是不固定的长度 2-7
static NSRegularExpression* regex;
if (!regex) {
NSString* pattern = @"[a-zA-Z]";
regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
}
NSInteger numMatch = [regex numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, len)];
return len - (numMatch ) / 2;
}
- (NSString *)mt_safeSubStringWithStringLength:(NSInteger)maxLength
{
return [self mt_safeSubStringWithByteLength:maxLength isCaculateCustomByteLength:NO];
}
- (NSString *)mt_safeSubStringWithByteLength:(NSInteger)maxLength
{
return [self mt_safeSubStringWithByteLength:maxLength isCaculateCustomByteLength:YES];
}
- (NSString *)mt_safeSubStringWithByteLength:(NSInteger)maxLength isCaculateCustomByteLength:(BOOL)isCaculateCustomByteLength
{
NSString * textString = self;
NSRange fullRange = NSMakeRange(0, [textString length]);
__block NSMutableString * resultString = [NSMutableString string];
NSInteger currentLength = isCaculateCustomByteLength ? textString.mt_customByteLength :textString.length;
if (currentLength > maxLength) {
[textString enumerateSubstringsInRange:fullRange
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
// NSLog(@"%@ %@", substring, NSStringFromRange(substringRange));
NSMutableString * toString = [resultString mutableCopy];
[toString appendString:substring];
NSInteger currentLength = isCaculateCustomByteLength ? toString.mt_customByteLength :toString.length;
if (currentLength <= maxLength) {
resultString = toString;
} else {
*stop = YES;
}
}];
textString = [resultString copy];
}
return textString;
}
//判断字符串的字符个数 Unicode编码后是按4个字符算的
- (NSInteger)mt_unicodeCharacterByteLength
{
NSString * tempStr = self;
int strlength = 0;
char* p = (char*)[tempStr cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[tempStr lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
- (BOOL)mt_stringContainsEmoji
{
// 过滤所有表情。returnValue为NO表示不含有表情YES表示含有表情
__block BOOL returnValue = NO;
[self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
@end