Skip to content

Commit

Permalink
pod support
Browse files Browse the repository at this point in the history
  • Loading branch information
molon committed Oct 15, 2015
1 parent ef7f05c commit 64d94e0
Showing 184 changed files with 2,088 additions and 1,103 deletions.
1 change: 1 addition & 0 deletions MLEmoji/MLEmojiLabel.h → Classes/MLEmojiLabel.h
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ typedef NS_OPTIONS(NSUInteger, MLEmojiLabelLinkType) {

@property (nonatomic, copy) NSString *customEmojiRegex; //自定义表情正则
@property (nonatomic, copy) NSString *customEmojiPlistName; //xxxxx.plist 格式
@property (nonatomic, copy) NSString *customEmojiBundleName; //自定义表情图片所存储的bundleName xxxx.bundle格式

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-property-synthesis"
159 changes: 92 additions & 67 deletions MLEmoji/MLEmojiLabel.m → Classes/MLEmojiLabel.m
Original file line number Diff line number Diff line change
@@ -83,18 +83,18 @@ + (instancetype)sharedInstance {
#pragma mark - getter
- (NSMutableDictionary *)emojiDictRecords
{
if (!_emojiDictRecords) {
_emojiDictRecords = [NSMutableDictionary new];
}
return _emojiDictRecords;
if (!_emojiDictRecords) {
_emojiDictRecords = [NSMutableDictionary new];
}
return _emojiDictRecords;
}

- (NSMutableDictionary *)emojiRegularExpressions
{
if (!_emojiRegularExpressions) {
_emojiRegularExpressions = [NSMutableDictionary new];
}
return _emojiRegularExpressions;
if (!_emojiRegularExpressions) {
_emojiRegularExpressions = [NSMutableDictionary new];
}
return _emojiRegularExpressions;
}

#pragma mark - common
@@ -135,9 +135,14 @@ - (NSRegularExpression *)regularExpressionForRegex:(NSString*)regex

@interface TTTAttributedLabel(MLEmojiLabel)

@property (readwrite, nonatomic, strong) TTTAttributedLabelLink *activeLink;

- (void)commonInit;
- (NSArray *)addLinksWithTextCheckingResults:(NSArray *)results
attributes:(NSDictionary *)attributes;
- (void)drawStrike:(CTFrameRef)frame
inRect:(CGRect)rect
context:(CGContextRef)c;

@end

@@ -159,11 +164,11 @@ @implementation MLEmojiLabel
+ (NSDictionary *)emojiDictionary {
static NSDictionary *emojiDictionary = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *emojiFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"expressionImage.plist"];
emojiDictionary = [[NSDictionary alloc] initWithContentsOfFile:emojiFilePath];
});
return emojiDictionary;
dispatch_once(&onceToken, ^{
NSString *emojiFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MLEmoji_ExpressionImage.plist"];
emojiDictionary = [[NSDictionary alloc] initWithContentsOfFile:emojiFilePath];
});
return emojiDictionary;
}

#pragma mark - 表情 callback
@@ -174,22 +179,22 @@ + (NSDictionary *)emojiDictionary {
} CustomGlyphMetrics, *CustomGlyphMetricsRef;

static void deallocCallback(void *refCon) {
free(refCon), refCon = NULL;
free(refCon), refCon = NULL;
}

static CGFloat ascentCallback(void *refCon) {
CustomGlyphMetricsRef metrics = (CustomGlyphMetricsRef)refCon;
return metrics->ascent;
CustomGlyphMetricsRef metrics = (CustomGlyphMetricsRef)refCon;
return metrics->ascent;
}

static CGFloat descentCallback(void *refCon) {
CustomGlyphMetricsRef metrics = (CustomGlyphMetricsRef)refCon;
return metrics->descent;
CustomGlyphMetricsRef metrics = (CustomGlyphMetricsRef)refCon;
return metrics->descent;
}

static CGFloat widthCallback(void *refCon) {
CustomGlyphMetricsRef metrics = (CustomGlyphMetricsRef)refCon;
return metrics->width;
CustomGlyphMetricsRef metrics = (CustomGlyphMetricsRef)refCon;
return metrics->width;
}

#pragma mark - 初始化和TTT的一些修正
@@ -256,10 +261,12 @@ static inline CGFloat TTTFlushFactorForTextAlignment(NSTextAlignment textAlignme
}

#pragma mark - 绘制表情
- (void)drawOtherForEndWithFrame:(CTFrameRef)frame
inRect:(CGRect)rect
context:(CGContextRef)c
- (void)drawStrike:(CTFrameRef)frame
inRect:(CGRect)rect
context:(CGContextRef)c
{
[super drawStrike:frame inRect:rect context:c];

//PS:这个是在TTT里drawFramesetter....方法最后做了修改的基础上。
CGFloat emojiWith = self.font.lineHeight*kEmojiWidthRatioWithLineHeight;
CGFloat emojiOriginYOffset = self.font.lineHeight*kEmojiOriginYOffsetRatioWithLineHeight;
@@ -352,7 +359,8 @@ - (void)drawOtherForEndWithFrame:(CTFrameRef)frame
runBounds.origin.y = lineOrigins[lineIndex].y;
runBounds.origin.y -= runDescent;

UIImage *image = [UIImage imageNamed:imageName];
NSString *imagePath = [self.customEmojiBundleName?:@"MLEmoji_Expression.bundle" stringByAppendingPathComponent:imageName];
UIImage *image = [UIImage imageNamed:imagePath];
runBounds.origin.y -= emojiOriginYOffset; //稍微矫正下。
CGContextDrawImage(c, runBounds, image.CGImage);
}
@@ -394,18 +402,18 @@ - (NSMutableAttributedString*)mutableAttributeStringWithEmojiText:(NSAttributedS
for (NSTextCheckingResult *result in emojis) {
NSRange range = result.range;
NSAttributedString *attSubStr = [emojiText attributedSubstringFromRange:NSMakeRange(location, range.location - location)];
[attrStr appendAttributedString:attSubStr];
[attrStr appendAttributedString:attSubStr];

location = range.location + range.length;
location = range.location + range.length;

NSAttributedString *emojiKey = [emojiText attributedSubstringFromRange:range];
NSAttributedString *emojiKey = [emojiText attributedSubstringFromRange:range];

NSDictionary *emojiDict = self.customEmojiRegularExpression?self.customEmojiDictionary:[MLEmojiLabel emojiDictionary];

//如果当前获得key后面有多余的,这个需要记录下
NSAttributedString *otherAppendStr = nil;

NSString *imageName = emojiDict[emojiKey.string];
NSString *imageName = emojiDict[emojiKey.string];
if (!self.customEmojiRegularExpression) {
//微信的表情没有结束符号,所以有可能会发现过长的只有头部才是表情的段,需要循环检测一次。微信最大表情特殊字符是8个长度,检测8次即可
if (!imageName&&emojiKey.length>2) {
@@ -422,41 +430,41 @@ - (NSMutableAttributedString*)mutableAttributeStringWithEmojiText:(NSAttributedS
}
}

if (imageName) {
// 这里不用空格,空格有个问题就是连续空格的时候只显示在一行
NSMutableAttributedString *replaceStr = [[NSMutableAttributedString alloc] initWithString:kEmojiReplaceCharacter];
NSRange __range = NSMakeRange([attrStr length], 1);
[attrStr appendAttributedString:replaceStr];
if (imageName) {
// 这里不用空格,空格有个问题就是连续空格的时候只显示在一行
NSMutableAttributedString *replaceStr = [[NSMutableAttributedString alloc] initWithString:kEmojiReplaceCharacter];
NSRange __range = NSMakeRange([attrStr length], 1);
[attrStr appendAttributedString:replaceStr];
if (otherAppendStr) { //有其他需要添加的
[attrStr appendAttributedString:otherAppendStr];
}

// 定义回调函数
CTRunDelegateCallbacks callbacks;
callbacks.version = kCTRunDelegateCurrentVersion;
callbacks.getAscent = ascentCallback;
callbacks.getDescent = descentCallback;
callbacks.getWidth = widthCallback;
callbacks.dealloc = deallocCallback;
// 定义回调函数
CTRunDelegateCallbacks callbacks;
callbacks.version = kCTRunDelegateCurrentVersion;
callbacks.getAscent = ascentCallback;
callbacks.getDescent = descentCallback;
callbacks.getWidth = widthCallback;
callbacks.dealloc = deallocCallback;

// 这里设置下需要绘制的图片的大小,这里我自定义了一个结构体以便于存储数据
CustomGlyphMetricsRef metrics = malloc(sizeof(CustomGlyphMetrics));
// 这里设置下需要绘制的图片的大小,这里我自定义了一个结构体以便于存储数据
CustomGlyphMetricsRef metrics = malloc(sizeof(CustomGlyphMetrics));
metrics->width = emojiWith;
metrics->ascent = 1/(1+kAscentDescentScale)*metrics->width;
metrics->descent = metrics->ascent*kAscentDescentScale;
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, metrics);
[attrStr addAttribute:(NSString *)kCTRunDelegateAttributeName
metrics->ascent = 1/(1+kAscentDescentScale)*metrics->width;
metrics->descent = metrics->ascent*kAscentDescentScale;
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, metrics);
[attrStr addAttribute:(NSString *)kCTRunDelegateAttributeName
value:(__bridge id)delegate
range:__range];
CFRelease(delegate);
CFRelease(delegate);

// 设置自定义属性,绘制的时候需要用到
[attrStr addAttribute:kCustomGlyphAttributeImageName
// 设置自定义属性,绘制的时候需要用到
[attrStr addAttribute:kCustomGlyphAttributeImageName
value:imageName
range:__range];
} else {
[attrStr appendAttributedString:emojiKey];
}
} else {
[attrStr appendAttributedString:emojiKey];
}
}
if (location < [emojiText length]) {
NSRange range = NSMakeRange(location, [emojiText length] - location);
@@ -596,39 +604,56 @@ - (void)setCustomEmojiPlistName:(NSString *)customEmojiPlistName
_customEmojiPlistName = customEmojiPlistName;

if (customEmojiPlistName&&customEmojiPlistName.length>0) {
self.customEmojiDictionary = [[MLEmojiLabelRegexPlistManager sharedInstance]emojiDictForKey:customEmojiPlistName];
self.customEmojiDictionary = [[MLEmojiLabelRegexPlistManager sharedInstance]emojiDictForKey:customEmojiPlistName];
}else{
self.customEmojiDictionary = nil;
}

self.text = self.emojiText; //简单重新绘制处理下
}

- (void)setCustomEmojiBundleName:(NSString *)customEmojiBundleName
{
if (customEmojiBundleName&&customEmojiBundleName.length>0&&![[customEmojiBundleName lowercaseString] hasSuffix:@".bundle"]) {
customEmojiBundleName = [customEmojiBundleName stringByAppendingString:@".bundle"];
}

_customEmojiBundleName = customEmojiBundleName;

self.text = self.emojiText; //简单重新绘制处理下
}

- (void)setFont:(UIFont *)font
{
[super setFont:font];
self.text = self.emojiText; //简单重新绘制处理下
}

#pragma mark - select link override
//PS:此处是在TTT代码里添加一个供继承的行为
- (BOOL)didSelectLinkWithTextCheckingResult:(NSTextCheckingResult*)result

- (void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (result.resultType == NSTextCheckingTypeCorrection) {
//判断消息类型
for (NSUInteger i=0; i<kURLActionCount; i++) {
if ([result.replacementString hasPrefix:kURLActions[i]]) {
NSString *content = [result.replacementString substringFromIndex:kURLActions[i].length];
if(self.delegate&&[self.delegate respondsToSelector:@selector(mlEmojiLabel:didSelectLink:withType:)]){
//如果delegate实现了mlEmojiLabel自身的选择link方法
if(self.delegate&&[self.delegate respondsToSelector:@selector(mlEmojiLabel:didSelectLink:withType:)]){
if (self.activeLink&&self.activeLink.result.resultType==NSTextCheckingTypeCorrection) {
NSTextCheckingResult *result = self.activeLink.result;

//判断消息类型
for (NSUInteger i=0; i<kURLActionCount; i++) {
if ([result.replacementString hasPrefix:kURLActions[i]]) {
NSString *content = [result.replacementString substringFromIndex:kURLActions[i].length];
//type的数组和i刚好对应
[self.delegate mlEmojiLabel:self didSelectLink:content withType:i];
return YES;

self.activeLink = nil;
return;
}
return NO;
}
}
}
return NO;

[super touchesEnded:touches withEvent:event];
}

#pragma mark - UIResponderStandardEditActions
@@ -644,7 +669,7 @@ - (void)copy:(__unused id)sender {
}
}

#pragma mark - other
//#pragma mark - other
//为了生成plist方便的一个方法罢了
//- (void)initPlist
//{
@@ -655,7 +680,7 @@ - (void)copy:(__unused id)sender {
// [testArray addObject:[testString substringWithRange:result.range]];
// [testDict setObject:[NSString stringWithFormat:@"Expression_%u",testArray.count] forKey:[testString substringWithRange:result.range]];
// }];
//
//
// NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// NSString *doc = [NSString stringWithFormat:@"%@/expression.plist",documentDir];
// NSLog(@"%@,length:%u",doc,testArray.count);
@@ -666,7 +691,7 @@ - (void)copy:(__unused id)sender {
// if ([testDict writeToFile:doc atomically:YES]) {
// NSLog(@"归档到expressionImage.plist成功");
// }
//
//
// // NSString *testString = @"[微笑][撇嘴][色][发呆][得意][流泪][害羞][闭嘴][睡][大哭][尴尬][发怒][调皮][呲牙][惊讶][难过][酷][冷汗][抓狂][吐][偷笑][愉快][白眼][傲慢][饥饿][困][惊恐][流汗][憨笑][悠闲][奋斗][咒骂][疑问][嘘][晕][疯了][衰][骷髅][敲打][再见][擦汗][抠鼻][鼓掌][糗大了][坏笑][左哼哼][右哼哼][哈欠][鄙视][委屈][快哭了][阴险][亲亲][吓][可怜][菜刀][西瓜][啤酒][篮球][乒乓][咖啡][饭][猪头][玫瑰][凋谢][嘴唇][爱心][心碎][蛋糕][闪电][炸弹][刀][足球][瓢虫][便便][月亮][太阳][礼物][拥抱][强][弱][握手][胜利][抱拳][勾引][拳头][差劲][爱你][NO][OK][爱情][飞吻][跳跳][发抖][怄火][转圈][磕头][回头][跳绳][投降]";
// // NSMutableArray *testArray = [NSMutableArray array];
// // NSMutableDictionary *testDict = [NSMutableDictionary dictionary];
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 64d94e0

Please sign in to comment.