聊天输入框,可以输入表情

This commit is contained in:
liyuhua
2024-05-17 15:06:46 +08:00
parent d22d0aa9fe
commit 82a476fd46
9 changed files with 105 additions and 6 deletions

View File

@@ -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 = "<group>"; };
234E50A92BF5F95A005CB6D5 /* MSSessionPublicChatHalIRightmageCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSSessionPublicChatHalIRightmageCell.h; sourceTree = "<group>"; };
234E50AA2BF5F95A005CB6D5 /* MSSessionPublicChatHalIRightmageCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSSessionPublicChatHalIRightmageCell.m; sourceTree = "<group>"; };
234E50AD2BF7352C005CB6D5 /* NSTextAttachment+MSImage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSTextAttachment+MSImage.h"; sourceTree = "<group>"; };
234E50AE2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSTextAttachment+MSImage.m"; sourceTree = "<group>"; };
234F44E22B3EA4F900E2B532 /* PILineManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PILineManager.swift; sourceTree = "<group>"; };
235714582BE8BC6C004C81D6 /* MSSessionPublicChatHallVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSSessionPublicChatHallVC.h; sourceTree = "<group>"; };
235714592BE8BC6C004C81D6 /* MSSessionPublicChatHallVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSSessionPublicChatHallVC.m; sourceTree = "<group>"; };
@@ -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 = "<group>";
};
234E50AC2BF734FC005CB6D5 /* NSTextAttachment */ = {
isa = PBXGroup;
children = (
234E50AD2BF7352C005CB6D5 /* NSTextAttachment+MSImage.h */,
234E50AE2BF7352C005CB6D5 /* NSTextAttachment+MSImage.m */,
);
path = NSTextAttachment;
sourceTree = "<group>";
};
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 */,

View File

@@ -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<NSAttributedStringKey,id> * _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

View File

@@ -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];
}
//

View File

@@ -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];
}
//

View File

@@ -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];
}
//

View File

@@ -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];
}
//

View File

@@ -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 */

View File

@@ -0,0 +1,16 @@
//
// NSTextAttachment+MSImage.h
// YuMi
//
// Created by duoban on 2024/5/17.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSTextAttachment (MSImage)
@property(nonatomic,copy) NSString *imageName;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,17 @@
//
// NSTextAttachment+MSImage.m
// YuMi
//
// Created by duoban on 2024/5/17.
//
#import "NSTextAttachment+MSImage.h"
#import <objc/runtime.h>
@implementation NSTextAttachment (MSImage)
-(NSString *)imageName{
return objc_getAssociatedObject(self, @"kTextAttachmentImageName");
}
-(void)setImageName:(NSString *)imageName{
objc_setAssociatedObject(self, @"kTextAttachmentImageName", imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end