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

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:50:43
字體:
來源:轉載
供稿:網友

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

微信表情輸入,iOS

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

具體的實現就不細說了,主要功能就是點擊表情時,將對應表情的圖片名稱通知給delegate。

二、實現表情textView(WKExpressionTextView)

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

//富文本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 *insertAttributeStr = [NSAttributedString attributedStringWithAttachment:attachment];NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];//在當前編輯位置插入字符串[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字段,為了保存表情對應的文本,用于復制粘貼操作。

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

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

主要方法

+ (NSAttributedString *)generateAttributeStringWithOriginalString:(NSString *)originalString fontSize:(CGFloat)fontSize{NSError *error = NULL;NSMutableAttributedString *resultAttrString = [[NSMutableAttributedString alloc] initWithString:originalString];NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"//[[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 對應字體大小** @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;}

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

編輯是應該對應selectedRange

復制粘貼操作需要重新實現

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

對應selectedRange操作

具體的操作查看源碼

重新實現copy、cut方法

進行復制、粘貼操作會發現,不能對圖片進行復制,所以需要自己重寫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];}

記錄默認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高仿微信表情輸入功能代碼分享,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云霄县| 大新县| 合江县| 会昌县| 饶河县| 重庆市| 梁平县| 乌兰浩特市| 北碚区| 泰来县| 凤山市| 昔阳县| 嫩江县| 锦州市| 陵川县| 区。| 邻水| 栖霞市| 浑源县| 西宁市| 鄱阳县| 宝鸡市| 奈曼旗| 康马县| 安福县| 桂东县| 彰化县| 会理县| 甘孜县| 石阡县| 咸宁市| 会理县| 雷州市| 义乌市| 永新县| 鹤庆县| 常熟市| 平和县| 临泽县| 右玉县| 庄河市|