From 82a476fd46a78577494c2e76616a06d9228785af Mon Sep 17 00:00:00 2001 From: liyuhua <15626451870@163.com> Date: Fri, 17 May 2024 15:06:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E8=BE=93=E5=85=A5=E6=A1=86?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=BE=93=E5=85=A5=E8=A1=A8=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YuMi.xcodeproj/project.pbxproj | 14 +++++++++++ .../InputView/InputView/UITextView+QEmotion.m | 23 +++++++++++++++++-- .../View/Session/MSSessionPublicChatHallVC.m | 10 +++++++- .../View/Session/SessionViewController.m | 10 +++++++- .../XPMomentsSimpleDetailViewController.m | 10 +++++++- .../View/XPMonentsDetailViewController.m | 10 +++++++- YuMi/Structure/PrefixHeader.pch | 1 + .../NSTextAttachment+MSImage.h | 16 +++++++++++++ .../NSTextAttachment+MSImage.m | 17 ++++++++++++++ 9 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.h create mode 100644 YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.m diff --git a/YuMi.xcodeproj/project.pbxproj b/YuMi.xcodeproj/project.pbxproj index 94ecd4fe..38c9d85e 100644 --- a/YuMi.xcodeproj/project.pbxproj +++ b/YuMi.xcodeproj/project.pbxproj @@ -214,6 +214,7 @@ 234E50A52BF5E556005CB6D5 /* MessageContentChatHallHeadView.m in Sources */ = {isa = PBXBuildFile; fileRef = 234E50A42BF5E556005CB6D5 /* MessageContentChatHallHeadView.m */; }; 234E50A82BF5EBDD005CB6D5 /* MSSessionPublicChatHalRightTextCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 234E50A72BF5EBDD005CB6D5 /* MSSessionPublicChatHalRightTextCell.m */; }; 234E50AB2BF5F95A005CB6D5 /* MSSessionPublicChatHalIRightmageCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 234E50AA2BF5F95A005CB6D5 /* MSSessionPublicChatHalIRightmageCell.m */; }; + 234E50AF2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 234E50AE2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m */; }; 234F44E32B3EA4F900E2B532 /* PILineManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 234F44E22B3EA4F900E2B532 /* PILineManager.swift */; }; 2357145A2BE8BC6C004C81D6 /* MSSessionPublicChatHallVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 235714592BE8BC6C004C81D6 /* MSSessionPublicChatHallVC.m */; }; 235714602BE8BD5C004C81D6 /* MSSessionScrollingModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 2357145F2BE8BD5C004C81D6 /* MSSessionScrollingModel.m */; }; @@ -1951,6 +1952,8 @@ 234E50A72BF5EBDD005CB6D5 /* MSSessionPublicChatHalRightTextCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSSessionPublicChatHalRightTextCell.m; sourceTree = ""; }; 234E50A92BF5F95A005CB6D5 /* MSSessionPublicChatHalIRightmageCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSSessionPublicChatHalIRightmageCell.h; sourceTree = ""; }; 234E50AA2BF5F95A005CB6D5 /* MSSessionPublicChatHalIRightmageCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSSessionPublicChatHalIRightmageCell.m; sourceTree = ""; }; + 234E50AD2BF7352C005CB6D5 /* NSTextAttachment+MSImage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSTextAttachment+MSImage.h"; sourceTree = ""; }; + 234E50AE2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSTextAttachment+MSImage.m"; sourceTree = ""; }; 234F44E22B3EA4F900E2B532 /* PILineManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PILineManager.swift; sourceTree = ""; }; 235714582BE8BC6C004C81D6 /* MSSessionPublicChatHallVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSSessionPublicChatHallVC.h; sourceTree = ""; }; 235714592BE8BC6C004C81D6 /* MSSessionPublicChatHallVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSSessionPublicChatHallVC.m; sourceTree = ""; }; @@ -4995,6 +4998,7 @@ 23E9E9AB2A81E8E900B792F2 /* Event */, E83645A42A40AEF600E0DBE4 /* Bundle */, E83645652A40A2CA00E0DBE4 /* CardManager */, + 234E50AC2BF734FC005CB6D5 /* NSTextAttachment */, 1427213629A75F6F00C7C423 /* CocoaHttpServer */, E8F61356291E269700E12650 /* Safe */, 9B09979F27F19D1C00EB8F14 /* QGVAPlayer */, @@ -5590,6 +5594,15 @@ path = UILabel; sourceTree = ""; }; + 234E50AC2BF734FC005CB6D5 /* NSTextAttachment */ = { + isa = PBXGroup; + children = ( + 234E50AD2BF7352C005CB6D5 /* NSTextAttachment+MSImage.h */, + 234E50AE2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m */, + ); + path = NSTextAttachment; + sourceTree = ""; + }; 234F44E12B3EA4DC00E2B532 /* YMLine */ = { isa = PBXGroup; children = ( @@ -12283,6 +12296,7 @@ E833ED0D274FAD1C00A2463B /* XPKickUserModel.m in Sources */, 9B42868E28C1AE2D009034D2 /* XPReceiveRedPacketView.m in Sources */, E8788934273A53D700BF1D57 /* XPSendGiftView.m in Sources */, + 234E50AF2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m in Sources */, E896EF9C2771AE6B00AD2CC1 /* XPMineFansViewController.m in Sources */, 238B37A72AC55A2C00BFC9D5 /* TreasureFairyRewardModel.m in Sources */, E84B0E462727EF9D008818C6 /* XPRoomMessageParser.m in Sources */, diff --git a/YuMi/CustomUI/InputView/InputView/UITextView+QEmotion.m b/YuMi/CustomUI/InputView/InputView/UITextView+QEmotion.m index 12d23569..38f2b5ae 100644 --- a/YuMi/CustomUI/InputView/InputView/UITextView+QEmotion.m +++ b/YuMi/CustomUI/InputView/InputView/UITextView+QEmotion.m @@ -82,7 +82,7 @@ } - (NSString *)normalText { - NSMutableString *normalMutableString = self.attributedText.string.mutableCopy; + NSMutableString *normalMutableString = [[NSMutableString alloc]initWithString:[self getResutlText]]; CGSize maxSize = CGSizeMake(50, MAXFLOAT); YYTextLayout * layout = [YYTextLayout layoutWithContainerSize:maxSize text:self.attributedText]; for (int i = 0; i< layout.attachments.count; i++) { @@ -96,5 +96,24 @@ } return normalMutableString; } - +-(NSString *)getResutlText{ + if (self.attributedText.length <= 0) { + return @""; + } + NSMutableAttributedString *resutlAtt = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText ]; + //枚举出所有的附件字符串 + __block NSUInteger base = 0; + [self.attributedText enumerateAttributesInRange:NSMakeRange(0, self.attributedText.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) { + NSTextAttachment *textAtt = attrs[@"NSAttachment"]; + //获取当前替换字符串的长度 + NSInteger length = range.location + base + range.length; + if (textAtt && length <= resutlAtt.length) { + [resutlAtt replaceCharactersInRange:NSMakeRange(range.location + base, range.length) + withString:textAtt.imageName]; + base += textAtt.imageName.length - 1; + + } + }]; + return resutlAtt.string; +} @end diff --git a/YuMi/Modules/YMMessage/View/Session/MSSessionPublicChatHallVC.m b/YuMi/Modules/YMMessage/View/Session/MSSessionPublicChatHallVC.m index f3cc6c47..fc26fd0b 100644 --- a/YuMi/Modules/YMMessage/View/Session/MSSessionPublicChatHallVC.m +++ b/YuMi/Modules/YMMessage/View/Session/MSSessionPublicChatHallVC.m @@ -897,7 +897,15 @@ */ - (void)emotionView:(QEmotionBoardView *)emotionView didSelectEmotion:(QEmotion *)emotion atIndex:(NSInteger)index { //把😊插入到输入栏 - [_inputBarView insertEmotion:emotion.displayName]; + UIFont *font = [UIFont systemFontOfSize:17.5]; + NSTextAttachment * emotionAtt = [[NSTextAttachment alloc] init]; + UIImage *iconImage = emotion.image; + emotionAtt.bounds = CGRectMake(0, roundf(font.capHeight - emotion.image.size.width)/2.f, emotion.image.size.width, emotion.image.size.height); + emotionAtt.image = iconImage; + emotionAtt.imageName = emotion.displayName; + NSAttributedString *emotionAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)emotionAtt]; + + [_inputBarView insertEmotionAttributedString:emotionAttribute]; } // 删除按钮的点击事件回调 diff --git a/YuMi/Modules/YMMessage/View/Session/SessionViewController.m b/YuMi/Modules/YMMessage/View/Session/SessionViewController.m index 39d8c5f4..b5f5645e 100644 --- a/YuMi/Modules/YMMessage/View/Session/SessionViewController.m +++ b/YuMi/Modules/YMMessage/View/Session/SessionViewController.m @@ -1022,7 +1022,15 @@ */ - (void)emotionView:(QEmotionBoardView *)emotionView didSelectEmotion:(QEmotion *)emotion atIndex:(NSInteger)index { //把😊插入到输入栏 - [_inputBarView insertEmotion:emotion.displayName]; + UIFont *font = [UIFont systemFontOfSize:17.5]; + NSTextAttachment * emotionAtt = [[NSTextAttachment alloc] init]; + UIImage *iconImage = emotion.image; + emotionAtt.bounds = CGRectMake(0, roundf(font.capHeight - emotion.image.size.width)/2.f, emotion.image.size.width, emotion.image.size.height); + emotionAtt.image = iconImage; + emotionAtt.imageName = emotion.displayName; + NSAttributedString *emotionAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)emotionAtt]; + + [_inputBarView insertEmotionAttributedString:emotionAttribute]; } // 删除按钮的点击事件回调 diff --git a/YuMi/Modules/YMMonents/View/XPMomentsSimpleDetailViewController.m b/YuMi/Modules/YMMonents/View/XPMomentsSimpleDetailViewController.m index d5d8c40c..9fe55a9b 100644 --- a/YuMi/Modules/YMMonents/View/XPMomentsSimpleDetailViewController.m +++ b/YuMi/Modules/YMMonents/View/XPMomentsSimpleDetailViewController.m @@ -427,7 +427,15 @@ */ - (void)emotionView:(QEmotionBoardView *)emotionView didSelectEmotion:(QEmotion *)emotion atIndex:(NSInteger)index { //把😊插入到输入栏 - [_inputBarView insertEmotion:emotion.displayName]; + UIFont *font = [UIFont systemFontOfSize:17.5]; + NSTextAttachment * emotionAtt = [[NSTextAttachment alloc] init]; + UIImage *iconImage = emotion.image; + emotionAtt.bounds = CGRectMake(0, roundf(font.capHeight - emotion.image.size.width)/2.f, emotion.image.size.width, emotion.image.size.height); + emotionAtt.image = iconImage; + emotionAtt.imageName = emotion.displayName; + NSAttributedString *emotionAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)emotionAtt]; + + [_inputBarView insertEmotionAttributedString:emotionAttribute]; } // 删除按钮的点击事件回调 diff --git a/YuMi/Modules/YMMonents/View/XPMonentsDetailViewController.m b/YuMi/Modules/YMMonents/View/XPMonentsDetailViewController.m index fb43ec92..fd9c3da3 100644 --- a/YuMi/Modules/YMMonents/View/XPMonentsDetailViewController.m +++ b/YuMi/Modules/YMMonents/View/XPMonentsDetailViewController.m @@ -390,7 +390,15 @@ */ - (void)emotionView:(QEmotionBoardView *)emotionView didSelectEmotion:(QEmotion *)emotion atIndex:(NSInteger)index { //把😊插入到输入栏 - [_inputBarView insertEmotion:emotion.displayName]; + UIFont *font = [UIFont systemFontOfSize:17.5]; + NSTextAttachment * emotionAtt = [[NSTextAttachment alloc] init]; + UIImage *iconImage = emotion.image; + emotionAtt.bounds = CGRectMake(0, roundf(font.capHeight - emotion.image.size.width)/2.f, emotion.image.size.width, emotion.image.size.height); + emotionAtt.image = iconImage; + emotionAtt.imageName = emotion.displayName; + NSAttributedString *emotionAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)emotionAtt]; + + [_inputBarView insertEmotionAttributedString:emotionAttribute]; } // 删除按钮的点击事件回调 diff --git a/YuMi/Structure/PrefixHeader.pch b/YuMi/Structure/PrefixHeader.pch index eb62529c..19612bfc 100644 --- a/YuMi/Structure/PrefixHeader.pch +++ b/YuMi/Structure/PrefixHeader.pch @@ -62,4 +62,5 @@ isEnterprise = [bundleID isEqualToString:@"com.hflighting.yumi"];\ #import "MSBaseRTLFlowLayout.h" #import "MSBaseTextField.h" #import "SZTextView.h" +#import "NSTextAttachment+MSImage.h" #endif /* PrefixHeader_pch */ diff --git a/YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.h b/YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.h new file mode 100644 index 00000000..5bdd533a --- /dev/null +++ b/YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.h @@ -0,0 +1,16 @@ +// +// NSTextAttachment+MSImage.h +// YuMi +// +// Created by duoban on 2024/5/17. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface NSTextAttachment (MSImage) +@property(nonatomic,copy) NSString *imageName; +@end + +NS_ASSUME_NONNULL_END diff --git a/YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.m b/YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.m new file mode 100644 index 00000000..200393ef --- /dev/null +++ b/YuMi/Tools/NSTextAttachment/NSTextAttachment+MSImage.m @@ -0,0 +1,17 @@ +// +// NSTextAttachment+MSImage.m +// YuMi +// +// Created by duoban on 2024/5/17. +// + +#import "NSTextAttachment+MSImage.h" +#import +@implementation NSTextAttachment (MSImage) +-(NSString *)imageName{ + return objc_getAssociatedObject(self, @"kTextAttachmentImageName"); +} +-(void)setImageName:(NSString *)imageName{ + objc_setAssociatedObject(self, @"kTextAttachmentImageName", imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} +@end