国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS高仿微信表情輸入功能代碼分享

2020-07-26 03:05:31
字體:
供稿:網(wǎng)友

最近項目需求,要實現(xiàn)一個類似微信的的表情輸入,于是把微信的表情扒拉出來,實現(xiàn)了一把。可以從這里下載源碼。看起來表情輸入沒有多少東西,不外乎就是用NSTextAttachment來實現(xiàn)圖文混排,結(jié)果在實現(xiàn)的過程中遇到了很多小問題,接下來會一一介紹遇到過的坑。先上一張效果圖:

一、實現(xiàn)表情選擇View(WKExpressionView)

具體的實現(xiàn)就不細(xì)說了,主要功能就是點擊表情時,將對應(yīng)表情的圖片名稱通知給delegate。

二、實現(xiàn)表情textView(WKExpressionTextView)

WKExpressionTextView繼承自UITextView, 提供
- (void)setExpressionWithImageName:(NSString *)imageName fontSize:(CGFloat)fontSize方法,用于根據(jù)圖片插入表情。 具體實現(xiàn):

//富文本W(wǎng)KExpressionTextAttachment *attachment = [[WKExpressionTextAttachment alloc] initWithData:nil ofType:nil];UIImage *image = [UIImage imageNamed:imageName];attachment.image = image;attachment.text = [WKExpressionTool getExpressionStringWithImageName:imageName];attachment.bounds = CGRectMake(0, 0, fontSize, fontSize);NSAttributedString *insertAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];//在當(dāng)前編輯位置插入字符串[resultAttrString insertAttributedString:insertAttributeStr atIndex:self.selectedRange.location];NSRange tempRange = self.selectedRange;self.attributedText = resultAttrString;self.selectedRange = NSMakeRange(tempRange.location + 1, 0);[self.textStorage addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:_defaultFontSize]} range:NSMakeRange(0, self.attributedText.length)];[self scrollRangeToVisible:self.selectedRange];[self textChanged];

其中WKExpressionTextAttachment繼承自NSTextAttachment, 并新增text字段,為了保存表情對應(yīng)的文本,用于復(fù)制粘貼操作。

@interface WKExpressionTextAttachment : NSTextAttachment@property (nonatomic, copy) NSString *text;@end

WKExpressionTool的提供將普通字符串轉(zhuǎn)換為富文本的方法,主要用于復(fù)制時生成表情。

主要方法

+ (NSAttributedString *)generateAttributeStringWithOriginalString:(NSString *)originalString fontSize:(CGFloat)fontSize{NSError *error = NULL;NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithString:originalString];NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http://[[a-zA-Z0-9/u4e00-/u9fa5]{1,}//]" options:NSRegularExpressionAllowCommentsAndWhitespace error:&error];NSArray *results = [regex matchesInString:originalString options:NSMatchingReportCompletion range:NSMakeRange(0, originalString.length)];if (results) {for (NSTextCheckingResult *result in results.reverseObjectEnumerator) {NSRange resultRange = [result rangeAtIndex:0];NSString *stringResult = [originalString substringWithRange:resultRange];NSLog(@"%s %@/n", __FUNCTION__, stringResult);NSAttributedString *expressionAttrString = [self getAttributeStringWithExpressionString:stringResult fontSize:fontSize];[resultAttrString replaceCharactersInRange:resultRange withAttributedString:expressionAttrString];}}return resultAttrString;}/*** 通過表情生成富文本** @param expressionString 表情名* @param fontSize 對應(yīng)字體大小** @return 富文本*/+ (NSAttributedString *)getAttributeStringWithExpressionString:(NSString *)expressionString fontSize:(CGFloat)fontSize{NSString *imageName = [self getExpressionStringWithImageName:expressionString];WKExpressionTextAttachment *attachment = [[WKExpressionTextAttachment alloc] initWithData:nil ofType:nil];UIImage *image = [UIImage imageNamed:imageName];attachment.image = image;attachment.text = [WKExpressionTool getExpressionStringWithImageName:imageName];attachment.bounds = CGRectMake(0, 0, fontSize, fontSize);NSAttributedString *appendAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];return appendAttributeStr;}

至此,基本功能實現(xiàn)完成。 接下來說說遇到的小問題

編輯是應(yīng)該對應(yīng)selectedRange

復(fù)制粘貼操作需要重新實現(xiàn)

textView在插入NSTextAttachment后,會默認(rèn)把font的size修改為12,需要記錄默認(rèn)的size

對應(yīng)selectedRange操作

具體的操作查看源碼

重新實現(xiàn)copy、cut方法

進(jìn)行復(fù)制、粘貼操作會發(fā)現(xiàn),不能對圖片進(jìn)行復(fù)制,所以需要自己重寫copy、cut方法

- (void)copy:(id)sender{NSAttributedString *selectedString = [self.attributedText attributedSubstringFromRange:self.selectedRange];NSString *copyString = [self parseAttributeTextToNormalString:selectedString];UIPasteboard *pboard = [UIPasteboard generalPasteboard];if (copyString.length != 0) {pboard.string = copyString;}}- (void)cut:(id)sender{[self copy:sender];NSMutableAttributedString *originalString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];[originalString deleteCharactersInRange:self.selectedRange];self.attributedText = originalString;NSLog(@"--%@", NSStringFromRange(self.selectedRange));[self textChanged];}

記錄默認(rèn)font的size

利用實例變量defaultFontSize,在WKExpressionTextView實例化時記錄self.font.pointSize,以后需要取font的size時,直接取defaultFontSize

@interface WKExpressionTextView : UITextView@property (nonatomic, assign) CGFloat defaultFontSize;@end@implementation WKExpressionTextView{CGFloat _defaultFontSize;}- (void)awakeFromNib{[self setup];}- (instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self) {[self setup];}return self;}- (void)setup{[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:self];_defaultFontSize = self.font.pointSize;self.delegate = self;}

以上所述是小編給大家介紹的iOS高仿微信表情輸入功能代碼分享,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂托克旗| 沙田区| 阳泉市| 安图县| 汉沽区| 巴南区| 梧州市| 册亨县| 乌苏市| 万州区| 芮城县| 曲松县| 伽师县| 谷城县| 读书| 万载县| 乌鲁木齐市| 安溪县| 元阳县| 格尔木市| 张家港市| 饶平县| 定结县| 浠水县| 长宁县| 岳阳县| 平阳县| 墨玉县| 阳新县| 诸暨市| 静宁县| 汉川市| 陇西县| 崇阳县| 仲巴县| 色达县| 苍溪县| 龙川县| 田林县| 涟水县| 香河县|