// // GDYTranslateTool.m // iphoneLive // // Created by YB007 on 2022/4/8. // Copyright © 2022 cat. All rights reserved. // #import "GDYTranslateTool.h" @implementation GDYTranslateTool // 接口语言标识 +(NSString *)serviceLang;{ if ([lagType isEqual:ZH_CN]) { return @"zh-cn"; } return @"en"; } /// 是否包含中文 +(BOOL)isChinese:(NSString *)str{ for(int i=0; i< [str length];i++){ int a = [str characterAtIndex:i]; if( a > 0x4E00 && a < 0x9FFF){ return YES; } } return NO; } /// 纯数字 +(BOOL)isPureNumandCharacters:(NSString *)string{ string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]]; if(string.length > 0) { return NO; } return YES; } /// 是否包含英文字符 +(BOOL)isContainedEn:(NSString *)str { NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil]; NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)]; //count是str中包含[A-Za-z]数字的个数,只要count>0,说明str中包含数字 if (count > 0) { return YES; } return NO; } /// 纯标点 -(BOOL)isAllCharacterString:(NSString *)string{ NSString *regex = @"[~`!@#$%^&*()_+-=[]|{};':\",./<>?]{,}/";//规定的特殊字符,可以自己随意添加 //计算字符串长度 NSInteger str_length = [string length]; NSInteger allIndex = 0; for (int i = 0; i\n%@",attstr); //遍历所有的result 取出range for (NSTextCheckingResult *result in resultArr) { //取出图片名 NSString *imageName = [originStr substringWithRange:NSMakeRange(result.range.location, result.range.length)]; //NSLog(@"--------%@",imageName); /* NSTextAttachment *attach = [[NSTextAttachment alloc] init]; UIImage *emojiImage = [UIImage imageNamed:imageName]; NSAttributedString *imageString; if (emojiImage) { attach.image = emojiImage; attach.bounds = CGRectMake(0, -2, 15, 15); imageString = [NSAttributedString attributedStringWithAttachment:attach]; }else{ imageString = [[NSMutableAttributedString alloc]initWithString:imageName]; } */ NSAttributedString *imageString = [[NSMutableAttributedString alloc]initWithString:@""]; //图片附件的文本长度是1 //NSLog(@"emoj===%zd===size-w:%f==size-h:%f",imageString.length,imageString.size.width,imageString.size.height); NSUInteger length = attstr.length; NSRange newRange = NSMakeRange(result.range.location - lengthDetail, result.range.length); [attstr replaceCharactersInRange:newRange withAttributedString:imageString]; lengthDetail += length - attstr.length; } NSString *newStr = [attstr string]; //NSLog(@"del emoji\n原:%@\n新:%@\n富:%@\n长:%lu",originStr,newStr,attstr,(unsigned long)attstr.length); return newStr; } @end