| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /************************************************************
- * * Hyphenate CONFIDENTIAL
- * __________________
- * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
- *
- * NOTICE: All information contained herein is, and remains
- * the property of Hyphenate Inc.
- * Dissemination of this information or reproduction of this material
- * is strictly forbidden unless prior written permission is obtained
- * from Hyphenate Inc.
- */
- #import "EaseEmotionEscape.h"
- #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
- #define kEmotionTopMargin -3.0f
- @implementation EMTextAttachment
- //I want my emoticon has the same size with line's height
- - (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0)
- {
- return CGRectMake( 0, kEmotionTopMargin, lineFrag.size.height, lineFrag.size.height);
- }
- @end
- @interface EaseEmotionEscape ()
- {
- NSString *_urlPattern;
- NSDictionary *_dict;
- }
- @end
- @implementation EaseEmotionEscape
- static EaseEmotionEscape *_sharedInstance = nil;
- + (EaseEmotionEscape *)sharedInstance
- {
- if (_sharedInstance == nil)
- {
- @synchronized(self) {
- _sharedInstance = [[EaseEmotionEscape alloc] init];
- }
- }
- return _sharedInstance;
- }
- + (NSMutableAttributedString *) attributtedStringFromText:(NSString *) aInputText
- {
- return nil;
- }
- + (NSAttributedString *) attStringFromTextForChatting:(NSString *) aInputText
- {
- return nil;
- }
- + (NSAttributedString *) attStringFromTextForInputView:(NSString *) aInputText
- {
- return nil;
- }
- - (NSMutableAttributedString *) attributtedStringFromText:(NSString *) aInputText
- {
- if (_urlPattern == nil || _urlPattern.length == 0) {
- NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:aInputText attributes:nil ];
- return string;
- }
- NSString *urlPattern = _urlPattern;
- NSError *error = nil;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlPattern options:NSRegularExpressionCaseInsensitive error:&error ];
-
- NSArray* matches = [regex matchesInString:aInputText options:NSMatchingReportCompletion range:NSMakeRange(0, [aInputText length])];
- NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:aInputText attributes:nil ];
-
- for (NSTextCheckingResult *match in [matches reverseObjectEnumerator]) {
- NSRange matchRange = [match range];
- NSString *subStr = [aInputText substringWithRange:matchRange];
-
- EMTextAttachment * textAttachment = [[EMTextAttachment alloc ] initWithData:nil ofType:nil];
- textAttachment.imageName = subStr;
- UIImage * emojiImage;
- NSString *emojiName = @"";
- if (_dict) {
- emojiName = [_dict objectForKey:subStr];
- }
-
- if (emojiName == nil || emojiName.length == 0) {
- emojiName = subStr;
- }
-
- emojiImage = [UIImage imageNamed:emojiName];
-
- NSAttributedString * textAttachmentString;
- if (emojiImage) {
- textAttachment.image = emojiImage ;
- textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
- }else{
- NSString *str = [self getEmojiTextByKey:subStr];
- if (str != nil) {
- str = [NSString stringWithFormat:@"[%@]", str];
- textAttachmentString = [[NSAttributedString alloc] initWithString:str];
- }else {
- textAttachmentString = [[NSAttributedString alloc] initWithString:@"[表情]"];
- }
- }
-
- if (textAttachment != nil) {
- [string deleteCharactersInRange:matchRange];
- [string insertAttributedString:textAttachmentString atIndex:matchRange.location];
- }
- }
-
- return string;
- }
- - (NSAttributedString *) attStringFromTextForChatting:(NSString *) aInputText textFont:(UIFont*)font
- {
- NSMutableAttributedString * string = [self attributtedStringFromText:aInputText];
- // NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- // paragraphStyle.lineSpacing = 0.0;
- // [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
- if (font) {
- [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
- }
- return string;
- }
- - (NSAttributedString *) attStringFromTextForInputView:(NSString *) aInputText textFont:(UIFont*)font
- {
- NSMutableAttributedString * string = [self attributtedStringFromText:aInputText];
- // NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- // paragraphStyle.lineSpacing = 0.0;
- // [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
- if (font) {
- [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
- }
- return string;
- }
- - (NSString*) getEmojiTextByKey:(NSString*) aKey
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *plistPaht = [paths objectAtIndex:0];
- NSString *fileName = [plistPaht stringByAppendingPathComponent:@"EmotionTextMapList.plist"];
- NSMutableDictionary *emojiKeyValue = [[NSMutableDictionary alloc] initWithContentsOfFile: fileName];
- return [emojiKeyValue objectForKey:aKey];
- // NSLog(@"write data is :%@",writeData);
- }
- - (NSString*) getEmojiImageNameByKey:(NSString*) aKey
- {
- return nil;
- }
- - (void) setEaseEmotionEscapePattern:(NSString *)pattern
- {
- _urlPattern = pattern;
- }
- - (void) setEaseEmotionEscapeDictionary:(NSDictionary*)dict
- {
- _dict = dict;
- }
- @end
|