单军华
2018-07-11 7b02207537d35bfa1714bf8beafc921f717d100a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/************************************************************
 *  * 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