Commit 2b090369f4f39c5994ec1cd941eac4fade60fbc7
1 parent
f53e8099
0.1.18 ,增加正则表情的处理
Showing
8 changed files
with
164 additions
and
3 deletions
CNLiveBusinessTools.podspec
| ... | ... | @@ -8,7 +8,7 @@ |
| 8 | 8 | |
| 9 | 9 | Pod::Spec.new do |s| |
| 10 | 10 | s.name = 'CNLiveBusinessTools' |
| 11 | - s.version = '0.1.17' | |
| 11 | + s.version = '0.1.18' | |
| 12 | 12 | s.summary = '业务工具集合组件.' |
| 13 | 13 | |
| 14 | 14 | # This description is used to generate tags and improve search results. |
| ... | ... | @@ -46,5 +46,5 @@ Pod::Spec.new do |s| |
| 46 | 46 | s.dependency 'CNLiveBaseKit' |
| 47 | 47 | s.dependency 'CNLiveRequestBastKit' |
| 48 | 48 | s.dependency 'CNLiveTripartiteManagement/QMUIKit' |
| 49 | - | |
| 49 | + s.dependency 'CNLiveTripartiteManagement/YYKit' | |
| 50 | 50 | end | ... | ... |
CNLiveBusinessTools/Classes/CNLiveBusinessTools.h
| ... | ... | @@ -97,6 +97,15 @@ typedef NS_ENUM(NSInteger, CNFileType) { |
| 97 | 97 | */ |
| 98 | 98 | + (NSString *)chatBackgroupImagefilePath; |
| 99 | 99 | #pragma mark -- |
| 100 | + | |
| 101 | +/// 文字正则匹配为emoji表情后转成富文本 | |
| 102 | +/// @param text 文字内容 | |
| 103 | ++ (NSMutableAttributedString *)replaceQQemojiWithCustomText:(NSString *)text; | |
| 104 | + | |
| 105 | +/// (IM专用)文字正则匹配为emoji表情后转成富文本 | |
| 106 | +/// @param text 文字内容 | |
| 107 | ++ (NSMutableAttributedString *)replaceNativeQQemojiWithCustomText:(NSString *)text; | |
| 108 | + | |
| 100 | 109 | @end |
| 101 | 110 | |
| 102 | 111 | NS_ASSUME_NONNULL_END | ... | ... |
CNLiveBusinessTools/Classes/CNLiveBusinessTools.m
| ... | ... | @@ -11,6 +11,10 @@ |
| 11 | 11 | #import <CNLiveUserManagement/CNUserInfoManager.h> |
| 12 | 12 | #import "CommonCrypto/CommonDigest.h" |
| 13 | 13 | #import <CNLiveBaseKit/CNLiveBaseKit.h> |
| 14 | +#import "CNEmojiTextAttachment.h" | |
| 15 | +#import <QMUIKit/QMUIEmotionView.h> | |
| 16 | +#import "CNLiveQQEmotionManager.h" | |
| 17 | +#import <YYKit/YYKit.h> | |
| 14 | 18 | @implementation CNLiveBusinessTools |
| 15 | 19 | |
| 16 | 20 | + (NSString *)cnStatWithType:(CNStatType)statType type:(NSString *)type userId:(NSString *)userId programId:(NSString *)programId title:(NSString *)title { |
| ... | ... | @@ -636,4 +640,113 @@ |
| 636 | 640 | return filePath; |
| 637 | 641 | |
| 638 | 642 | } |
| 643 | + | |
| 644 | ++ (NSMutableAttributedString *)replaceQQemojiWithCustomText:(NSString *)text | |
| 645 | +{ | |
| 646 | + if (text == nil || [text isEqualToString:@""]) return [[NSMutableAttributedString alloc] initWithString:@""]; | |
| 647 | + //过滤未知字符 | |
| 648 | + text = [NSString filterUnknownChara:text]; | |
| 649 | + | |
| 650 | + //利用正则 取出 [开头 ]结尾 的数组,内容通配 ,再次forin QQEmojiArr,得到真正的表情个数,再替换删除 | |
| 651 | + NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text]; | |
| 652 | + | |
| 653 | + NSString *zhengze = @"\\[(.*?)\\]"; | |
| 654 | + NSError *error; | |
| 655 | + NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:zhengze options:NSRegularExpressionCaseInsensitive error:&error]; | |
| 656 | + if (!re) { | |
| 657 | + NSLog(@"正则表达式匹配错误%@",[error localizedDescription]); | |
| 658 | + } | |
| 659 | + NSArray * arr = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)]; | |
| 660 | + if (!arr.count) {//说明字符串中没有表情通配符,是普通的文本 | |
| 661 | + | |
| 662 | + }else{//有表情通配符 | |
| 663 | + //如果有多个表情图,必须从后往前替换,因为替换后Range就不准确了 | |
| 664 | + NSArray<QMUIEmotion *> *emojiArr = [CNLiveQQEmotionManager emotionsForQQ]; | |
| 665 | + for (int i = (int)arr.count - 1; i >= 0; i --) { | |
| 666 | + //NSTextCheckingResult里面包含range | |
| 667 | + NSTextCheckingResult * result = arr[i]; | |
| 668 | + | |
| 669 | + for (int j = 0; j < emojiArr.count; j ++) { | |
| 670 | + if ([[text substringWithRange:result.range] isEqualToString:emojiArr[j].displayName]) { | |
| 671 | + @try { | |
| 672 | + if (![NSThread isMainThread]) { | |
| 673 | + dispatch_sync(dispatch_get_main_queue(), ^{ | |
| 674 | + YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:emojiArr[j].image]; | |
| 675 | + imageView.width = 24*RATIO; | |
| 676 | + imageView.height = 24*RATIO; | |
| 677 | + NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:UIFontCNMake(contentLabelFontSize) alignment:YYTextVerticalAlignmentCenter]; | |
| 678 | + [attStr replaceCharactersInRange:result.range withAttributedString:attachText]; | |
| 679 | + }); | |
| 680 | + }else{ | |
| 681 | + YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:emojiArr[j].image]; | |
| 682 | + imageView.width = 24*RATIO; | |
| 683 | + imageView.height = 24*RATIO; | |
| 684 | + NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:UIFontCNMake(contentLabelFontSize) alignment:YYTextVerticalAlignmentCenter]; | |
| 685 | + | |
| 686 | + [attStr replaceCharactersInRange:result.range withAttributedString:attachText]; | |
| 687 | + } | |
| 688 | + } @catch (NSException *exception) { | |
| 689 | + NSLog(@"获取的异常为=>%@",exception.reason); | |
| 690 | + } | |
| 691 | + } | |
| 692 | + } | |
| 693 | + } | |
| 694 | + } | |
| 695 | + return attStr; | |
| 696 | +} | |
| 697 | + | |
| 698 | ++ (NSMutableAttributedString *)replaceNativeQQemojiWithCustomText:(NSString *)text | |
| 699 | +{ | |
| 700 | + if (text == nil || [text isEqualToString:@""]) return [[NSMutableAttributedString alloc] initWithString:@""]; | |
| 701 | + // 过滤未知字符 | |
| 702 | + text = [NSString filterUnknownChara:text]; | |
| 703 | + //利用正则 取出 [开头 ]结尾 的数组,内容通配 ,再次forin QQEmojiArr,得到真正的表情个数,再替换删除 | |
| 704 | + NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text]; | |
| 705 | + NSString *zhengze = @"\\[(.*?)\\]"; | |
| 706 | + NSError *error; | |
| 707 | + NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:zhengze options:NSRegularExpressionCaseInsensitive error:&error]; | |
| 708 | + if (!re) { | |
| 709 | + NSLog(@"正则表达式匹配错误%@",[error localizedDescription]); | |
| 710 | + } | |
| 711 | + NSArray * arr = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)]; | |
| 712 | + if (!arr.count) {//说明字符串中没有表情通配符,是普通的文本 | |
| 713 | + | |
| 714 | + }else | |
| 715 | + {//有表情通配符 | |
| 716 | + //如果有多个表情图,必须从后往前替换,因为替换后Range就不准确了 | |
| 717 | + NSArray<QMUIEmotion *> *emojiArr = [CNLiveQQEmotionManager emotionsForQQ]; | |
| 718 | + for (int i = (int)arr.count - 1; i >= 0; i --) { | |
| 719 | + //NSTextCheckingResult里面包含range | |
| 720 | + NSTextCheckingResult * result = arr[i]; | |
| 721 | + | |
| 722 | + if ([[[text substringWithRange:result.range] substringToIndex:1] isEqualToString:@"["]) { | |
| 723 | + //可能自定义表情 | |
| 724 | + for (int j = 0; j < emojiArr.count; j ++) { | |
| 725 | + if ([[text substringWithRange:result.range] isEqualToString:emojiArr[j].displayName]) { | |
| 726 | + | |
| 727 | + NSString *emojiText = [text substringWithRange:result.range]; | |
| 728 | + | |
| 729 | + CNEmojiTextAttachment *textAttachment = [[CNEmojiTextAttachment alloc]init]; | |
| 730 | + textAttachment.image = emojiArr[j].image; | |
| 731 | + textAttachment.bounds = CGRectMake(0, -4.7, 20*RATIO, 20*RATIO); | |
| 732 | + textAttachment.emojiText = emojiText; | |
| 733 | + | |
| 734 | + NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment]; | |
| 735 | + [attStr replaceCharactersInRange:result.range withAttributedString:imageStr]; | |
| 736 | + NSLog(@"\n11111:text%@,range:%@",text,NSStringFromRange(result.range)); | |
| 737 | + } | |
| 738 | + } | |
| 739 | + }else | |
| 740 | + {//目前只有 暂时没有判断是否是url办法 表情和url | |
| 741 | + //url | |
| 742 | + NSString *urlStr = [text substringWithRange:result.range]; | |
| 743 | + NSMutableAttributedString *urlAttStr = [[NSMutableAttributedString alloc] initWithString:urlStr attributes:@{NSFontAttributeName:UIFontMake(contentLabelFontSize)}]; | |
| 744 | + [urlAttStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:urlStr] range:NSMakeRange(0, urlStr.length)]; | |
| 745 | + [attStr replaceCharactersInRange:result.range withAttributedString:urlAttStr]; | |
| 746 | + } | |
| 747 | + } | |
| 748 | + } | |
| 749 | + return attStr; | |
| 750 | +} | |
| 751 | + | |
| 639 | 752 | @end | ... | ... |
CNLiveBusinessTools/Classes/CNLiveQQEmotionManager/CNEmojiTextAttachment.h
0 → 100644
| 1 | +// | |
| 2 | +// CNEmojiTextAttachment.h | |
| 3 | +// CNLiveBusinessTools | |
| 4 | +// | |
| 5 | +// Created by 张旭 on 2020/4/20. | |
| 6 | +// | |
| 7 | + | |
| 8 | +#import <UIKit/UIKit.h> | |
| 9 | + | |
| 10 | +NS_ASSUME_NONNULL_BEGIN | |
| 11 | + | |
| 12 | +@interface CNEmojiTextAttachment : NSTextAttachment | |
| 13 | + | |
| 14 | +/** | |
| 15 | + 保存emojiText的值 | |
| 16 | + */ | |
| 17 | +@property (nonatomic, strong) NSString *emojiText; | |
| 18 | + | |
| 19 | +@end | |
| 20 | + | |
| 21 | +NS_ASSUME_NONNULL_END | ... | ... |
CNLiveBusinessTools/Classes/CNLiveQQEmotionManager/CNEmojiTextAttachment.m
0 → 100644
Example/CNLiveBusinessTools.xcodeproj/project.pbxproj
| ... | ... | @@ -357,6 +357,7 @@ |
| 357 | 357 | "${BUILT_PRODUCTS_DIR}/CNLiveUserManagement/CNLiveUserManagement.framework", |
| 358 | 358 | "${BUILT_PRODUCTS_DIR}/MJExtension/MJExtension.framework", |
| 359 | 359 | "${BUILT_PRODUCTS_DIR}/QMUIKit/QMUIKit.framework", |
| 360 | + "${BUILT_PRODUCTS_DIR}/YYKit/YYKit.framework", | |
| 360 | 361 | ); |
| 361 | 362 | name = "[CP] Embed Pods Frameworks"; |
| 362 | 363 | outputPaths = ( |
| ... | ... | @@ -369,6 +370,7 @@ |
| 369 | 370 | "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CNLiveUserManagement.framework", |
| 370 | 371 | "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MJExtension.framework", |
| 371 | 372 | "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/QMUIKit.framework", |
| 373 | + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/YYKit.framework", | |
| 372 | 374 | ); |
| 373 | 375 | runOnlyForDeploymentPostprocessing = 0; |
| 374 | 376 | shellPath = /bin/sh; | ... | ... |
Example/CNLiveBusinessTools/CNLiveAppDelegate.m
| ... | ... | @@ -8,13 +8,15 @@ |
| 8 | 8 | |
| 9 | 9 | #import "CNLiveAppDelegate.h" |
| 10 | 10 | #import "CNLiveQQEmotionManager.h" |
| 11 | - | |
| 11 | +#import "CNLiveBusinessTools.h" | |
| 12 | 12 | @implementation CNLiveAppDelegate |
| 13 | 13 | |
| 14 | 14 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 15 | 15 | { |
| 16 | 16 | // Override point for customization after application launch. |
| 17 | 17 | [CNLiveQQEmotionManager emotionsForQQ]; |
| 18 | + NSMutableAttributedString *attStr = [CNLiveBusinessTools replaceQQemojiWithCustomText:@"asfa[微笑]"]; | |
| 19 | + NSLog(@"attStr:%@",attStr); | |
| 18 | 20 | return YES; |
| 19 | 21 | } |
| 20 | 22 | ... | ... |
Example/Podfile
| 1 | 1 | source 'http://bj.gitlab.cnlive.com/ios-team/CNLiveRepos.git' |
| 2 | +#source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git' | |
| 2 | 3 | source 'https://github.com/CocoaPods/Specs.git' |
| 3 | 4 | |
| 4 | 5 | use_frameworks! |
| ... | ... | @@ -13,6 +14,7 @@ target 'CNLiveBusinessTools_Example' do |
| 13 | 14 | pod 'CNLiveBaseKit' |
| 14 | 15 | pod 'CNLiveRequestBastKit' |
| 15 | 16 | pod 'CNLiveTripartiteManagement/QMUIKit' |
| 17 | + pod 'CNLiveTripartiteManagement/YYKit' | |
| 16 | 18 | |
| 17 | 19 | target 'CNLiveBusinessTools_Tests' do |
| 18 | 20 | inherit! :search_paths | ... | ... |