-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.m
89 lines (57 loc) · 2.68 KB
/
Utils.m
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
//
// Utils.m
// HWWeibo
//
// Created by gj on 15/8/24.
// Copyright (c) 2015年 www.huiwen.com 杭州汇文教育. All rights reserved.
//
#import "Utils.h"
#import "RegexKitLite.h"
@implementation Utils
+ (NSDate *)dateFromString:(NSString *)dateString withFormatterStr:(NSString *)formatterStr{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formatterStr];
NSDate *date = [formatter dateFromString:dateString];
return date;
}
+ (NSString *)stringFromDate:(NSDate *)date withFormmaterStr:(NSString *)formatterStr{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formatterStr];
NSString *dateString = [formatter stringFromDate:date];
return dateString;
}
+ (NSString *)weiboDateString:(NSString *)string{
NSString *formatterStr = @"E MMM dd HH:mm:ss Z yyyy";
NSDate *date = [Utils dateFromString:string withFormatterStr:formatterStr];
NSString *dateString = [Utils stringFromDate:date withFormmaterStr:@"MM-dd HH:mm"];
return dateString;
}
//处理文本中显示的图片
+ (NSString *)parseTextImage:(NSString *)text {
//[哈哈]--->图片名 ----> 替换成: <image url = '图片名'>
NSString *faceRegex = @"\\[\\w+\\]";
NSArray *faceItem = [text componentsMatchedByRegex:faceRegex];
//1>.读取emoticons.plist 表情配置文件
NSString *configPath = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
NSArray *faceConfig = [NSArray arrayWithContentsOfFile:configPath];
//2>.循环、遍历所有的查找出来的表情名:[哈哈]、[赞]、....
for (NSString *faceName in faceItem) {
//faceName = [哈哈]
//3.定义谓词条件,到emoticons.plist中查找表情名对应的表情item
NSString *t = [NSString stringWithFormat:@"self.chs='%@'",faceName];
NSPredicate *predicate = [NSPredicate predicateWithFormat:t];
NSArray *items = [faceConfig filteredArrayUsingPredicate:predicate];
if (items.count > 0) {
//4.取得过滤出来的表情item
NSDictionary *faceDic = items[0];
//5.取得图片名
NSString *imgName = faceDic[@"png"];
//6.构造表情表情 <image url = '图片名'>
NSString *replace = [NSString stringWithFormat:@"<image url = '%@'>",imgName];
//7.替换:将[哈哈] 替换成 <image url = '90.png'>
text = [text stringByReplacingOccurrencesOfString:faceName withString:replace];
}
}
return text;
}
@end